Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit88dcaf1

Browse files
author
Botao Xiao
committed
[Function add]: 1. Add leetcode solutions.
1 parentc28a128 commit88dcaf1

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+
classSolution {
55+
publicintsingleNumber(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
publicTrie() {
100100
this.root=newTrieNode();
101101
}
102-
102+
103103
/** Inserts a word into the trie.*/
104104
publicvoidinsert(Stringword) {
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
publicbooleansearch(Stringword) {
119119
if(word==null|| word.length()==0)returntrue;
@@ -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
publicbooleanstartsWith(Stringprefix) {
132132
if(prefix==null|| prefix.length()==0)returnfalse;
@@ -218,5 +218,67 @@ class Trie {
218218
*/
219219
```
220220

221+
###Fourth Time
222+
* Method 1: Tire Tree
223+
```Java
224+
classTrie {
225+
privatestaticclassNode{
226+
boolean isLeaf;
227+
Node[] childs;
228+
publicNode(){
229+
childs=newNode[26];
230+
}
231+
}
232+
privateNode root;
233+
/** Initialize your data structure here.*/
234+
publicTrie() {
235+
this.root=newNode();
236+
}
237+
238+
/** Inserts a word into the trie.*/
239+
publicvoidinsert(Stringword) {
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']=newNode();
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+
publicbooleansearch(Stringword) {
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)returnfalse;
258+
}
259+
return temp.isLeaf;
260+
}
261+
262+
/** Returns if there is any word in the trie that starts with the given prefix.*/
263+
publicbooleanstartsWith(Stringprefix) {
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)returnfalse;
269+
}
270+
returntrue;
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+
classSolution {
101+
publicint[]maxSlidingWindow(int[]nums,intk) {
102+
if(nums==null|| nums.length==0)returnnewint[0];
103+
LinkedList<Integer> list=newLinkedList<>();
104+
int[] res=newint[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+
publicclassSolutionextendsRelation {
45+
publicintfindCelebrity(intn) {
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))continueLABEL;
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+
classSolution {
48+
privateint[] uf;
49+
privateint[][] dir=newint[][]{{0,1}, {0,-1}, {-1,0}, {1,0}};
50+
publicList<Integer>numIslands2(intm,intn,int[][]positions) {
51+
this.uf=newint[m* n];
52+
Arrays.fill(uf,-1);
53+
List<Integer> result=newArrayList<>();
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+
privateintfind(inti){
72+
if(i!= uf[i])
73+
uf[i]= find(uf[i]);
74+
return uf[i];
75+
}
76+
privatebooleanunion(inti,intj){
77+
int p= find(i);
78+
int q= find(j);
79+
if(p!= q){
80+
uf[p]= q;
81+
returntrue;
82+
}
83+
returnfalse;
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+
classSolution {
21+
publicint[]nextGreaterElements(int[]nums) {
22+
int[] result=newint[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+
classSolution {
26+
privateint[] arr;
27+
publicintrotatedDigits(intN) {
28+
this.arr=newint[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+
privatebooleanisValid(intN){
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)returnfalse;
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)

[8]ページ先頭

©2009-2025 Movatter.jp