Skip to content

Commit d568e78

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 234fbba commit d568e78

5 files changed

+187
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,14 @@
455455

456456
[287. Find the Duplicate Number](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/287.%20Find%20the%20Duplicate%20Number.md)
457457

458+
[289. Game of Life](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/289.%20Game%20of%20Life.md)
459+
460+
[290. Word Pattern](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/290.%20Word%20Pattern.md)
461+
462+
[292. Nim Game](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/292.%20Nim%20Game.md)
463+
464+
[295.Find Median from Data Stream](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/295.Find%20Median%20from%20Data%20Stream.md)
465+
458466

459467
## Algorithm(4th_Edition)
460468
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.

leetcode/289. Game of Life.md

+78
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
## 289. Game of Life
2+
3+
### Question
4+
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
5+
6+
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
7+
8+
1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
9+
2. Any live cell with two or three live neighbors lives on to the next generation.
10+
3. Any live cell with more than three live neighbors dies, as if by over-population..
11+
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
12+
13+
Write a function to compute the next state (after one update) of the board given its current state. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
14+
15+
```
16+
Example:
17+
18+
Input:
19+
[
20+
[0,1,0],
21+
[0,0,1],
22+
[1,1,1],
23+
[0,0,0]
24+
]
25+
Output:
26+
[
27+
[0,0,0],
28+
[1,0,1],
29+
[0,1,1],
30+
[0,1,0]
31+
]
32+
```
33+
Follow up:
34+
* Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
35+
* In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
36+
37+
238
### Thinking:
339

440
```Java
@@ -46,4 +82,46 @@ class Solution {
4682
return count;
4783
}
4884
}
85+
```
86+
87+
### Second time
88+
1. I first updated the some of the cells first to indicate their next generation existance.
89+
2. Then I traversal the array agian and set them to correponding value.
90+
```Java
91+
class Solution {
92+
public void gameOfLife(int[][] board) {
93+
int height = board.length, width = board[0].length;
94+
for(int i = 0; i < height; i++){
95+
for(int j = 0; j < width; j++){
96+
int cur = board[i][j];
97+
int temp = checkNeighbour(board, i, j, height, width);
98+
if(cur == 1 && temp < 2) board[i][j] = 6;
99+
else if(cur == 0 && temp == 3) board[i][j] = 9;
100+
else if(cur == 1 && temp > 3) board[i][j] = 6;
101+
}
102+
}
103+
for(int i = 0; i < height; i++){
104+
for(int j = 0; j < width; j++){
105+
if(board[i][j] == 6) board[i][j] = 0;
106+
else if(board[i][j] == 9) board[i][j] = 1;
107+
}
108+
}
109+
}
110+
private int checkNeighbour(int[][] board, int row, int col, int height, int width){
111+
int count = 0;
112+
if(row + 1 < height){
113+
if(board[row + 1][col] == 1 || board[row + 1][col] == 6) count++;
114+
if(col - 1 >= 0 && (board[row + 1][col - 1] == 1 || board[row + 1][col - 1] == 6)) count ++;
115+
if(col + 1 < width && (board[row + 1][col + 1] == 1 || board[row + 1][col + 1] == 6)) count ++;
116+
}
117+
if(row - 1 >= 0){
118+
if(board[row - 1][col] == 1 || board[row - 1][col] == 6) count++;
119+
if(col - 1 >= 0 && (board[row - 1][col - 1] == 1 || board[row - 1][col - 1] == 6)) count ++;
120+
if(col + 1 < width && (board[row - 1][col + 1] == 1 || board[row - 1][col + 1] == 6)) count ++;
121+
}
122+
if(col + 1 < width && (board[row][col + 1] == 1 || board[row][col + 1] == 6)) count++;
123+
if(col - 1 >= 0 && (board[row][col - 1] == 1 || board[row][col - 1] == 6)) count++;
124+
return count;
125+
}
126+
}
49127
```

leetcode/290. Word Pattern.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 290. Word Pattern
2+
3+
### Question
4+
Given a pattern and a string str, find if str follows the same pattern.
5+
6+
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
7+
8+
```
9+
Example 1:
10+
11+
Input: pattern = "abba", str = "dog cat cat dog"
12+
Output: true
13+
14+
Example 2:
15+
16+
Input:pattern = "abba", str = "dog cat cat fish"
17+
Output: false
18+
19+
Example 3:
20+
21+
Input: pattern = "aaaa", str = "dog cat cat dog"
22+
Output: false
23+
24+
Example 4:
25+
26+
Input: pattern = "abba", str = "dog dog dog dog"
27+
Output: false
28+
29+
Notes:
30+
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
31+
```
32+
33+
### Second time:
34+
1. The key point of this question is the reflection between key and value.
35+
2. We need to mapping relationship is unique, so we can use HashMap, and use two methods: containsKey and containsValue.
36+
37+
```Java
38+
class Solution {
39+
public boolean wordPattern(String pattern, String str) {
40+
String[] arr = str.split(" ");
41+
char[] charArr = pattern.toCharArray();
42+
if(arr.length != charArr.length) return false;
43+
Map<String, Character> map = new HashMap<>();
44+
for(int i = 0; i < arr.length; i++){
45+
if(!map.containsKey(arr[i]) && !map.containsValue(charArr[i])){
46+
map.put(arr[i], charArr[i]);
47+
}else if(!map.containsKey(arr[i]) && map.containsValue(charArr[i])){
48+
return false;
49+
}else{
50+
if(charArr[i] != map.get(arr[i]))
51+
return false;
52+
}
53+
}
54+
return true;
55+
}
56+
}
57+
```

leetcode/292. Nim Game.md

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ No matter 1, 2, or 3 stones you remove, the last stone will always be removed by
1818
* Method 1:
1919
* 4的倍数无法获胜。
2020

21+
```Java
22+
class Solution {
23+
public boolean canWinNim(int n) {
24+
return n % 4 != 0;
25+
}
26+
}
27+
```
28+
29+
### Second time
2130
```Java
2231
class Solution {
2332
public boolean canWinNim(int n) {

leetcode/295.Find Median from Data Stream.md

+35
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,41 @@ class MedianFinder {
5151
}
5252
}
5353

54+
/**
55+
* Your MedianFinder object will be instantiated and called as such:
56+
* MedianFinder obj = new MedianFinder();
57+
* obj.addNum(num);
58+
* double param_2 = obj.findMedian();
59+
*/
60+
```
61+
62+
### Second time
63+
1. Use two priority queue to solve this quesiton.
64+
2. ![Imgur](https://i.imgur.com/wuuSz5M.jpg)
65+
```Java
66+
class MedianFinder {
67+
/** initialize your data structure here. */
68+
PriorityQueue<Integer> min;
69+
PriorityQueue<Integer> max;
70+
public MedianFinder() {
71+
min = new PriorityQueue<>();
72+
max = new PriorityQueue<>(new Comparator<Integer>(){
73+
@Override
74+
public int compare(Integer n1, Integer n2){
75+
return n2 - n1;
76+
}
77+
});
78+
}
79+
public void addNum(int num) {
80+
max.offer(num);
81+
min.offer(max.poll());
82+
if(max.size() < min.size()) max.offer(min.poll());
83+
}
84+
public double findMedian() {
85+
if(max.size() > min.size()) return (double)max.peek();
86+
else return (double)(max.peek() + min.peek()) / 2;
87+
}
88+
}
5489
/**
5590
* Your MedianFinder object will be instantiated and called as such:
5691
* MedianFinder obj = new MedianFinder();

0 commit comments

Comments
 (0)