Skip to content

Commit 88dcaf1

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions.
1 parent c28a128 commit 88dcaf1

8 files changed

+377
-4
lines changed

leetcode/136. Single Number.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@ class Solution {
4747
}
4848
}
4949
```
50+
51+
### Third Time
52+
* Method 1: Exclusive or
53+
```Java
54+
class Solution {
55+
public int singleNumber(int[] nums) {
56+
int res = nums[0];
57+
for(int i = 1; i < nums.length; i++){
58+
res ^= nums[i];
59+
}
60+
return res;
61+
}
62+
}
63+
```

leetcode/208. Implement Trie (Prefix Tree).md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Trie {
9999
public Trie() {
100100
this.root = new TrieNode();
101101
}
102-
102+
103103
/** Inserts a word into the trie. */
104104
public void insert(String word) {
105105
int len = word.length();
@@ -113,7 +113,7 @@ class Trie {
113113
}
114114
temp.isLeaf = true;
115115
}
116-
116+
117117
/** Returns if the word is in the trie. */
118118
public boolean search(String word) {
119119
if(word == null || word.length() == 0) return true;
@@ -126,7 +126,7 @@ class Trie {
126126
}
127127
return temp.isLeaf;
128128
}
129-
129+
130130
/** Returns if there is any word in the trie that starts with the given prefix. */
131131
public boolean startsWith(String prefix) {
132132
if(prefix == null || prefix.length() == 0) return false;
@@ -218,5 +218,67 @@ class Trie {
218218
*/
219219
```
220220

221+
### Fourth Time
222+
* Method 1: Tire Tree
223+
```Java
224+
class Trie {
225+
private static class Node{
226+
boolean isLeaf;
227+
Node[] childs;
228+
public Node(){
229+
childs = new Node[26];
230+
}
231+
}
232+
private Node root;
233+
/** Initialize your data structure here. */
234+
public Trie() {
235+
this.root = new Node();
236+
}
237+
238+
/** Inserts a word into the trie. */
239+
public void insert(String word) {
240+
char[] arr = word.toCharArray();
241+
Node temp = root;
242+
for(int i = 0; i < arr.length; i++){
243+
if(temp.childs[arr[i] - 'a'] == null){
244+
temp.childs[arr[i] - 'a'] = new Node();
245+
}
246+
temp = temp.childs[arr[i] - 'a'];
247+
}
248+
temp.isLeaf = true;
249+
}
250+
251+
/** Returns if the word is in the trie. */
252+
public boolean search(String word) {
253+
char[] arr = word.toCharArray();
254+
Node temp = root;
255+
for(int i = 0; i < arr.length; i++){
256+
temp = temp.childs[arr[i] - 'a'];
257+
if(temp == null) return false;
258+
}
259+
return temp.isLeaf;
260+
}
261+
262+
/** Returns if there is any word in the trie that starts with the given prefix. */
263+
public boolean startsWith(String prefix) {
264+
char[] arr = prefix.toCharArray();
265+
Node temp = root;
266+
for(int i = 0; i < arr.length; i++){
267+
temp = temp.childs[arr[i] - 'a'];
268+
if(temp == null) return false;
269+
}
270+
return true;
271+
}
272+
}
273+
274+
/**
275+
* Your Trie object will be instantiated and called as such:
276+
* Trie obj = new Trie();
277+
* obj.insert(word);
278+
* boolean param_2 = obj.search(word);
279+
* boolean param_3 = obj.startsWith(prefix);
280+
*/
281+
```
282+
221283
### Reference
222-
1. [Trie Tree 字典树](https://seanforfun.github.io/datastructure/2018/11/07/TrieTree.html)
284+
1. [Trie Tree 字典树](https://seanforfun.github.io/datastructure/2018/11/07/TrieTree.html)

leetcode/239. Sliding Window Maximum.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,25 @@ class Solution {
9393
}
9494
}
9595
```
96+
97+
### Third Time
98+
* Method 1: Deque
99+
```Java
100+
class Solution {
101+
public int[] maxSlidingWindow(int[] nums, int k) {
102+
if(nums == null || nums.length == 0) return new int[0];
103+
LinkedList<Integer> list = new LinkedList<>();
104+
int[] res = new int[nums.length - k + 1];
105+
for(int i = 0; i < nums.length; i++){
106+
while(!list.isEmpty() && nums[i] >= nums[list.getLast()]){
107+
list.pollLast();
108+
}
109+
list.add(i);
110+
if(list.get(0) < i - k + 1)
111+
list.pollFirst();
112+
if(i >= k - 1) res[i - k + 1] = nums[list.get(0)];
113+
}
114+
return res;
115+
}
116+
}
117+
```

leetcode/277. Find the Celebrity.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 277. Find the Celebrity
2+
3+
### Question
4+
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them.
5+
6+
Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
7+
8+
You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n). There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1.
9+
10+
11+
```
12+
Example 1:
13+
14+
Input: graph = [
15+
[1,1,0],
16+
[0,1,0],
17+
[1,1,1]
18+
]
19+
Output: 1
20+
Explanation: There are three persons labeled with 0, 1 and 2. graph[i][j] = 1 means person i knows person j, otherwise graph[i][j] = 0 means person i does not know person j. The celebrity is the person labeled as 1 because both 0 and 2 know him but 1 does not know anybody.
21+
22+
Example 2:
23+
24+
Input: graph = [
25+
[1,0,1],
26+
[1,1,0],
27+
[0,1,1]
28+
]
29+
Output: -1
30+
Explanation: There is no celebrity.
31+
```
32+
33+
Note:
34+
35+
The directed graph is represented as an adjacency matrix, which is an n x n matrix where a[i][j] = 1 means person i knows person j while a[i][j] = 0 means the contrary.
36+
Remember that you won't have direct access to the adjacency matrix.
37+
38+
### Solution
39+
* Method 1: O(n ^ 2)
40+
```Java
41+
/* The knows API is defined in the parent class Relation.
42+
boolean knows(int a, int b); */
43+
44+
public class Solution extends Relation {
45+
public int findCelebrity(int n) {
46+
LABEL:
47+
for(int i = 0; i < n; i++){
48+
for(int j = 0; j < n; j++){
49+
if(i == j) continue;
50+
if(knows(i, j) || !knows(j, i)) continue LABEL;
51+
}
52+
return i;
53+
}
54+
return -1;
55+
}
56+
}
57+
```

leetcode/305. Number of Islands II.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## 305. Number of Islands II
2+
3+
### Question
4+
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
5+
6+
```
7+
Example:
8+
9+
Input: m = 3, n = 3, positions = [[0,0], [0,1], [1,2], [2,1]]
10+
Output: [1,1,2,3]
11+
Explanation:
12+
13+
Initially, the 2d grid grid is filled with water. (Assume 0 represents water and 1 represents land).
14+
15+
0 0 0
16+
0 0 0
17+
0 0 0
18+
Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.
19+
20+
1 0 0
21+
0 0 0 Number of islands = 1
22+
0 0 0
23+
Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.
24+
25+
1 1 0
26+
0 0 0 Number of islands = 1
27+
0 0 0
28+
Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.
29+
30+
1 1 0
31+
0 0 1 Number of islands = 2
32+
0 0 0
33+
Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.
34+
35+
1 1 0
36+
0 0 1 Number of islands = 3
37+
0 1 0
38+
```
39+
40+
Follow up:
41+
42+
Can you do it in time complexity O(k log mn), where k is the length of the positions?
43+
44+
### Solution
45+
* Method 1: Union find
46+
```Java
47+
class Solution {
48+
private int[] uf;
49+
private int[][] dir = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
50+
public List<Integer> numIslands2(int m, int n, int[][] positions) {
51+
this.uf = new int[m * n];
52+
Arrays.fill(uf, -1);
53+
List<Integer> result = new ArrayList<>();
54+
int res = 0;
55+
for(int[] position : positions){
56+
res++;
57+
uf[position[0] * n + position[1]] = position[0] * n + position[1];
58+
int tx = 0, ty = 0;
59+
for(int d = 0; d < 4; d++){
60+
tx = position[0] + dir[d][0];
61+
ty = position[1] + dir[d][1];
62+
if(tx >= 0 && tx < m && ty >= 0 && ty < n && uf[tx * n + ty] != -1){
63+
if(union(position[0] * n + position[1], tx * n + ty))
64+
res--;
65+
}
66+
}
67+
result.add(res);
68+
}
69+
return result;
70+
}
71+
private int find(int i){
72+
if(i != uf[i])
73+
uf[i] = find(uf[i]);
74+
return uf[i];
75+
}
76+
private boolean union(int i, int j){
77+
int p = find(i);
78+
int q = find(j);
79+
if(p != q){
80+
uf[p] = q;
81+
return true;
82+
}
83+
return false;
84+
}
85+
}
86+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## 503. Next Greater Element II
2+
3+
### Question
4+
Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number.
5+
6+
```
7+
Example 1:
8+
Input: [1,2,1]
9+
Output: [2,-1,2]
10+
Explanation: The first 1's next greater number is 2;
11+
The number 2 can't find next greater number;
12+
The second 1's next greater number needs to search circularly, which is also 2.
13+
```
14+
15+
Note: The length of given array won't exceed 10000.
16+
17+
### Solution
18+
* Method 1: Array O(n^2)
19+
```Java
20+
class Solution {
21+
public int[] nextGreaterElements(int[] nums) {
22+
int[] result = new int[nums.length];
23+
for(int i = 0; i < nums.length; i++){
24+
int j = i + 1;
25+
while(j != i){
26+
if(j == nums.length){
27+
j = 0;
28+
if(j == i) break;
29+
}
30+
if(nums[j] > nums[i]){
31+
result[i] = nums[j];
32+
break;
33+
}
34+
j++;
35+
}
36+
if(j == i) result[i] = -1;
37+
}
38+
return result;
39+
}
40+
}
41+
```

leetcode/788. Rotated Digits.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 788. Rotated Digits
2+
3+
### Question
4+
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.
5+
6+
A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
7+
8+
Now given a positive number N, how many numbers X from 1 to N are good?
9+
10+
```
11+
Example:
12+
Input: 10
13+
Output: 4
14+
Explanation:
15+
There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
16+
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
17+
```
18+
19+
Note:
20+
1. N will be in range [1, 10000].
21+
22+
### Solution
23+
* Method 1: HashMap / Array
24+
```Java
25+
class Solution {
26+
private int[] arr;
27+
public int rotatedDigits(int N) {
28+
this.arr = new int[10];
29+
Arrays.fill(arr, -1);
30+
arr[0] = 0;
31+
arr[1] = 1;
32+
arr[8] = 8;
33+
arr[2] = 5;
34+
arr[5] = 2;
35+
arr[6] = 9;
36+
arr[9] = 6;
37+
int count = 0;
38+
for(int i = 1; i <= N; i++){
39+
if(isValid(i)) count++;
40+
}
41+
return count;
42+
}
43+
private boolean isValid(int N){
44+
int origin = N;
45+
int next = 0;
46+
int v = 1;
47+
while(N > 0){
48+
int cur = N % 10;
49+
if(arr[cur] == -1) return false;
50+
next = next + arr[cur] * v;
51+
N /= 10;
52+
v *= 10;
53+
}
54+
return origin != next;
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)