Skip to content

Commit aa6eb86

Browse files
committed
feat: update solutions to lc problems
1 parent 95d0209 commit aa6eb86

File tree

120 files changed

+143
-379
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+143
-379
lines changed

lcof2/剑指 Offer II 113. 课程顺序/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ class Solution:
9494
class Solution {
9595
public int[] findOrder(int numCourses, int[][] prerequisites) {
9696
List<Integer>[] g = new List[numCourses];
97-
for (int i = 0; i < numCourses; ++i) {
98-
g[i] = new ArrayList<>();
99-
}
97+
Arrays.setAll(g, k -> new ArrayList<>());
10098
int[] indeg = new int[numCourses];
10199
for (var p : prerequisites) {
102100
int a = p[0], b = p[1];

lcof2/剑指 Offer II 113. 课程顺序/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Solution {
22
public int[] findOrder(int numCourses, int[][] prerequisites) {
33
List<Integer>[] g = new List[numCourses];
4-
for (int i = 0; i < numCourses; ++i) {
5-
g[i] = new ArrayList<>();
6-
}
4+
Arrays.setAll(g, k -> new ArrayList<>());
75
int[] indeg = new int[numCourses];
86
for (var p : prerequisites) {
97
int a = p[0], b = p[1];

lcof2/剑指 Offer II 115. 重建序列/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ class Solution {
116116
int n = nums.length;
117117
int[] indeg = new int[n];
118118
List<Integer>[] g = new List[n];
119-
for (int i = 0; i < n; ++i) {
120-
g[i] = new ArrayList<>();
121-
}
119+
Arrays.setAll(g, k -> new ArrayList<>());
122120
for (int[] seq : sequences) {
123121
for (int i = 1; i < seq.length; ++i) {
124122
int a = seq[i - 1] - 1, b = seq[i] - 1;

lcof2/剑指 Offer II 115. 重建序列/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ public boolean sequenceReconstruction(int[] nums, int[][] sequences) {
33
int n = nums.length;
44
int[] indeg = new int[n];
55
List<Integer>[] g = new List[n];
6-
for (int i = 0; i < n; ++i) {
7-
g[i] = new ArrayList<>();
8-
}
6+
Arrays.setAll(g, k -> new ArrayList<>());
97
for (int[] seq : sequences) {
108
for (int i = 1; i < seq.length; ++i) {
119
int a = seq[i - 1] - 1, b = seq[i] - 1;

lcp/LCP 05. 发 LeetCoin/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ class Solution {
289289

290290
public int[] bonus(int n, int[][] leadership, int[][] operations) {
291291
g = new List[n + 1];
292-
for (int i = 0; i < g.length; ++i) {
293-
g[i] = new ArrayList<>();
294-
}
292+
Arrays.setAll(g, k -> new ArrayList<>());
295293
for (int[] l : leadership) {
296294
int a = l[0], b = l[1];
297295
g[a].add(b);

lcp/LCP 05. 发 LeetCoin/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ class Solution {
9696

9797
public int[] bonus(int n, int[][] leadership, int[][] operations) {
9898
g = new List[n + 1];
99-
for (int i = 0; i < g.length; ++i) {
100-
g[i] = new ArrayList<>();
101-
}
99+
Arrays.setAll(g, k -> new ArrayList<>());
102100
for (int[] l : leadership) {
103101
int a = l[0], b = l[1];
104102
g[a].add(b);

solution/0200-0299/0207.Course Schedule/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ class Solution:
8686
class Solution {
8787
public boolean canFinish(int numCourses, int[][] prerequisites) {
8888
List<Integer>[] g = new List[numCourses];
89-
for (int i = 0; i < numCourses; ++i) {
90-
g[i] = new ArrayList<>();
91-
}
89+
Arrays.setAll(g, k -> new ArrayList<>());
9290
int[] indeg = new int[numCourses];
9391
for (var p : prerequisites) {
9492
int a = p[0], b = p[1];

solution/0200-0299/0207.Course Schedule/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ class Solution:
7474
class Solution {
7575
public boolean canFinish(int numCourses, int[][] prerequisites) {
7676
List<Integer>[] g = new List[numCourses];
77-
for (int i = 0; i < numCourses; ++i) {
78-
g[i] = new ArrayList<>();
79-
}
77+
Arrays.setAll(g, k -> new ArrayList<>());
8078
int[] indeg = new int[numCourses];
8179
for (var p : prerequisites) {
8280
int a = p[0], b = p[1];

solution/0200-0299/0207.Course Schedule/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Solution {
22
public boolean canFinish(int numCourses, int[][] prerequisites) {
33
List<Integer>[] g = new List[numCourses];
4-
for (int i = 0; i < numCourses; ++i) {
5-
g[i] = new ArrayList<>();
6-
}
4+
Arrays.setAll(g, k -> new ArrayList<>());
75
int[] indeg = new int[numCourses];
86
for (var p : prerequisites) {
97
int a = p[0], b = p[1];

solution/0200-0299/0210.Course Schedule II/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ class Solution:
9191
class Solution {
9292
public int[] findOrder(int numCourses, int[][] prerequisites) {
9393
List<Integer>[] g = new List[numCourses];
94-
for (int i = 0; i < numCourses; ++i) {
95-
g[i] = new ArrayList<>();
96-
}
94+
Arrays.setAll(g, k -> new ArrayList<>());
9795
int[] indeg = new int[numCourses];
9896
for (var p : prerequisites) {
9997
int a = p[0], b = p[1];

solution/0200-0299/0210.Course Schedule II/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ class Solution:
8181
class Solution {
8282
public int[] findOrder(int numCourses, int[][] prerequisites) {
8383
List<Integer>[] g = new List[numCourses];
84-
for (int i = 0; i < numCourses; ++i) {
85-
g[i] = new ArrayList<>();
86-
}
84+
Arrays.setAll(g, k -> new ArrayList<>());
8785
int[] indeg = new int[numCourses];
8886
for (var p : prerequisites) {
8987
int a = p[0], b = p[1];

solution/0200-0299/0210.Course Schedule II/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Solution {
22
public int[] findOrder(int numCourses, int[][] prerequisites) {
33
List<Integer>[] g = new List[numCourses];
4-
for (int i = 0; i < numCourses; ++i) {
5-
g[i] = new ArrayList<>();
6-
}
4+
Arrays.setAll(g, k -> new ArrayList<>());
75
int[] indeg = new int[numCourses];
86
for (var p : prerequisites) {
97
int a = p[0], b = p[1];

solution/0300-0399/0310.Minimum Height Trees/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,8 @@ class Solution {
102102
return Collections.singletonList(0);
103103
}
104104
List<Integer>[] g = new List[n];
105+
Arrays.setAll(g, k -> new ArrayList<>());
105106
int[] degree = new int[n];
106-
for (int i = 0; i < n; ++i) {
107-
g[i] = new ArrayList<>();
108-
}
109107
for (int[] e : edges) {
110108
int a = e[0], b = e[1];
111109
g[a].add(b);

solution/0300-0399/0310.Minimum Height Trees/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@ class Solution {
8585
return Collections.singletonList(0);
8686
}
8787
List<Integer>[] g = new List[n];
88+
Arrays.setAll(g, k -> new ArrayList<>());
8889
int[] degree = new int[n];
89-
for (int i = 0; i < n; ++i) {
90-
g[i] = new ArrayList<>();
91-
}
9290
for (int[] e : edges) {
9391
int a = e[0], b = e[1];
9492
g[a].add(b);

solution/0300-0399/0310.Minimum Height Trees/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ public List<Integer> findMinHeightTrees(int n, int[][] edges) {
44
return Collections.singletonList(0);
55
}
66
List<Integer>[] g = new List[n];
7+
Arrays.setAll(g, k -> new ArrayList<>());
78
int[] degree = new int[n];
8-
for (int i = 0; i < n; ++i) {
9-
g[i] = new ArrayList<>();
10-
}
119
for (int[] e : edges) {
1210
int a = e[0], b = e[1];
1311
g[a].add(b);

solution/0400-0499/0444.Sequence Reconstruction/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ class Solution {
114114
int n = nums.length;
115115
int[] indeg = new int[n];
116116
List<Integer>[] g = new List[n];
117-
for (int i = 0; i < n; ++i) {
118-
g[i] = new ArrayList<>();
119-
}
117+
Arrays.setAll(g, k -> new ArrayList<>());
120118
for (var seq : sequences) {
121119
for (int i = 1; i < seq.size(); ++i) {
122120
int a = seq.get(i - 1) - 1, b = seq.get(i) - 1;

solution/0400-0499/0444.Sequence Reconstruction/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ class Solution {
103103
int n = nums.length;
104104
int[] indeg = new int[n];
105105
List<Integer>[] g = new List[n];
106-
for (int i = 0; i < n; ++i) {
107-
g[i] = new ArrayList<>();
108-
}
106+
Arrays.setAll(g, k -> new ArrayList<>());
109107
for (var seq : sequences) {
110108
for (int i = 1; i < seq.size(); ++i) {
111109
int a = seq.get(i - 1) - 1, b = seq.get(i) - 1;

solution/0400-0499/0444.Sequence Reconstruction/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ public boolean sequenceReconstruction(int[] nums, List<List<Integer>> sequences)
33
int n = nums.length;
44
int[] indeg = new int[n];
55
List<Integer>[] g = new List[n];
6-
for (int i = 0; i < n; ++i) {
7-
g[i] = new ArrayList<>();
8-
}
6+
Arrays.setAll(g, k -> new ArrayList<>());
97
for (var seq : sequences) {
108
for (int i = 1; i < seq.size(); ++i) {
119
int a = seq.get(i - 1) - 1, b = seq.get(i) - 1;

solution/0700-0799/0792.Number of Matching Subsequences/README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ class Solution:
152152
class Solution {
153153
public int numMatchingSubseq(String s, String[] words) {
154154
Deque<String>[] d = new Deque[26];
155-
for (int i = 0; i < 26; ++i) {
156-
d[i] = new ArrayDeque<>();
157-
}
155+
Arrays.setAll(d, k -> new ArrayDeque<>());
158156
for (String w : words) {
159157
d[w.charAt(0) - 'a'].add(w);
160158
}
@@ -179,9 +177,7 @@ class Solution {
179177
class Solution {
180178
public int numMatchingSubseq(String s, String[] words) {
181179
Deque<int[]>[] d = new Deque[26];
182-
for (int i = 0; i < 26; ++i) {
183-
d[i] = new ArrayDeque<>();
184-
}
180+
Arrays.setAll(d, k -> new ArrayDeque<>());
185181
for (int i = 0; i < words.length; ++i) {
186182
d[words[i].charAt(0) - 'a'].offer(new int[] {i, 0});
187183
}
@@ -208,9 +204,7 @@ class Solution {
208204
private List<Integer>[] d = new List[26];
209205

210206
public int numMatchingSubseq(String s, String[] words) {
211-
for (int i = 0; i < 26; ++i) {
212-
d[i] = new ArrayList<>();
213-
}
207+
Arrays.setAll(d, k -> new ArrayList<>());
214208
for (int i = 0; i < s.length(); ++i) {
215209
d[s.charAt(i) - 'a'].add(i);
216210
}

solution/0700-0799/0792.Number of Matching Subsequences/README_EN.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ class Solution:
103103
class Solution {
104104
public int numMatchingSubseq(String s, String[] words) {
105105
Deque<String>[] d = new Deque[26];
106-
for (int i = 0; i < 26; ++i) {
107-
d[i] = new ArrayDeque<>();
108-
}
106+
Arrays.setAll(d, k -> new ArrayDeque<>());
109107
for (String w : words) {
110108
d[w.charAt(0) - 'a'].add(w);
111109
}
@@ -130,9 +128,7 @@ class Solution {
130128
class Solution {
131129
public int numMatchingSubseq(String s, String[] words) {
132130
Deque<int[]>[] d = new Deque[26];
133-
for (int i = 0; i < 26; ++i) {
134-
d[i] = new ArrayDeque<>();
135-
}
131+
Arrays.setAll(d, k -> new ArrayDeque<>());
136132
for (int i = 0; i < words.length; ++i) {
137133
d[words[i].charAt(0) - 'a'].offer(new int[] {i, 0});
138134
}
@@ -159,9 +155,7 @@ class Solution {
159155
private List<Integer>[] d = new List[26];
160156

161157
public int numMatchingSubseq(String s, String[] words) {
162-
for (int i = 0; i < 26; ++i) {
163-
d[i] = new ArrayList<>();
164-
}
158+
Arrays.setAll(d, k -> new ArrayList<>());
165159
for (int i = 0; i < s.length(); ++i) {
166160
d[s.charAt(i) - 'a'].add(i);
167161
}

solution/0700-0799/0792.Number of Matching Subsequences/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Solution {
22
public int numMatchingSubseq(String s, String[] words) {
33
Deque<int[]>[] d = new Deque[26];
4-
for (int i = 0; i < 26; ++i) {
5-
d[i] = new ArrayDeque<>();
6-
}
4+
Arrays.setAll(d, k -> new ArrayDeque<>());
75
for (int i = 0; i < words.length; ++i) {
86
d[words[i].charAt(0) - 'a'].offer(new int[] {i, 0});
97
}

solution/0800-0899/0802.Find Eventual Safe States/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ class Solution {
122122
int n = graph.length;
123123
int[] indeg = new int[n];
124124
List<Integer>[] rg = new List[n];
125-
for (int i = 0; i < n; ++i) {
126-
rg[i] = new ArrayList<>();
127-
}
125+
Arrays.setAll(rg, k -> new ArrayList<>());
128126
Deque<Integer> q = new ArrayDeque<>();
129127
for (int i = 0; i < n; ++i) {
130128
for (int j : graph[i]) {

solution/0800-0899/0802.Find Eventual Safe States/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ class Solution {
9595
int n = graph.length;
9696
int[] indeg = new int[n];
9797
List<Integer>[] rg = new List[n];
98-
for (int i = 0; i < n; ++i) {
99-
rg[i] = new ArrayList<>();
100-
}
98+
Arrays.setAll(rg, k -> new ArrayList<>());
10199
Deque<Integer> q = new ArrayDeque<>();
102100
for (int i = 0; i < n; ++i) {
103101
for (int j : graph[i]) {

solution/0800-0899/0815.Bus Routes/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ class Solution {
120120
int n = routes.length;
121121
Set<Integer>[] s = new Set[n];
122122
List<Integer>[] g = new List[n];
123+
Arrays.setAll(s, k -> new HashSet<>());
124+
Arrays.setAll(g, k -> new ArrayList<>());
123125
Map<Integer, List<Integer>> d = new HashMap<>();
124126
for (int i = 0; i < n; ++i) {
125-
s[i] = new HashSet<>();
126-
g[i] = new ArrayList<>();
127127
for (int v : routes[i]) {
128128
s[i].add(v);
129129
d.computeIfAbsent(v, k -> new ArrayList<>()).add(i);

solution/0800-0899/0815.Bus Routes/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ class Solution {
9595
int n = routes.length;
9696
Set<Integer>[] s = new Set[n];
9797
List<Integer>[] g = new List[n];
98+
Arrays.setAll(s, k -> new HashSet<>());
99+
Arrays.setAll(g, k -> new ArrayList<>());
98100
Map<Integer, List<Integer>> d = new HashMap<>();
99101
for (int i = 0; i < n; ++i) {
100-
s[i] = new HashSet<>();
101-
g[i] = new ArrayList<>();
102102
for (int v : routes[i]) {
103103
s[i].add(v);
104104
d.computeIfAbsent(v, k -> new ArrayList<>()).add(i);

solution/0800-0899/0815.Bus Routes/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ public int numBusesToDestination(int[][] routes, int source, int target) {
66
int n = routes.length;
77
Set<Integer>[] s = new Set[n];
88
List<Integer>[] g = new List[n];
9+
Arrays.setAll(s, k -> new HashSet<>());
10+
Arrays.setAll(g, k -> new ArrayList<>());
911
Map<Integer, List<Integer>> d = new HashMap<>();
1012
for (int i = 0; i < n; ++i) {
11-
s[i] = new HashSet<>();
12-
g[i] = new ArrayList<>();
1313
for (int v : routes[i]) {
1414
s[i].add(v);
1515
d.computeIfAbsent(v, k -> new ArrayList<>()).add(i);

solution/0800-0899/0828.Count Unique Characters of All Substrings of a Given String/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class Solution:
8888
class Solution {
8989
public int uniqueLetterString(String s) {
9090
List<Integer>[] d = new List[26];
91+
Arrays.setAll(d, k -> new ArrayList<>());
9192
for (int i = 0; i < 26; ++i) {
92-
d[i] = new ArrayList<>();
9393
d[i].add(-1);
9494
}
9595
for (int i = 0; i < s.length(); ++i) {

solution/0800-0899/0828.Count Unique Characters of All Substrings of a Given String/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class Solution:
7474
class Solution {
7575
public int uniqueLetterString(String s) {
7676
List<Integer>[] d = new List[26];
77+
Arrays.setAll(d, k -> new ArrayList<>());
7778
for (int i = 0; i < 26; ++i) {
78-
d[i] = new ArrayList<>();
7979
d[i].add(-1);
8080
}
8181
for (int i = 0; i < s.length(); ++i) {

solution/0800-0899/0828.Count Unique Characters of All Substrings of a Given String/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public int uniqueLetterString(String s) {
33
List<Integer>[] d = new List[26];
4+
Arrays.setAll(d, k -> new ArrayList<>());
45
for (int i = 0; i < 26; ++i) {
5-
d[i] = new ArrayList<>();
66
d[i].add(-1);
77
}
88
for (int i = 0; i < s.length(); ++i) {

0 commit comments

Comments
 (0)