Skip to content

Commit c75df9f

Browse files
committed
feat: update solutions to lc problems
1 parent 36fb36c commit c75df9f

File tree

49 files changed

+441
-98
lines changed

Some content is hidden

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

49 files changed

+441
-98
lines changed

solution/0400-0499/0402.Remove K Digits/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class Solution {
9797
}
9898
int i = 0;
9999
for (; i < stk.length() && stk.charAt(i) == '0'; ++i) {
100-
101100
}
102101
String ans = stk.substring(i);
103102
return "".equals(ans) ? "0" : ans;

solution/0400-0499/0402.Remove K Digits/README_EN.md

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class Solution {
7979
}
8080
int i = 0;
8181
for (; i < stk.length() && stk.charAt(i) == '0'; ++i) {
82-
8382
}
8483
String ans = stk.substring(i);
8584
return "".equals(ans) ? "0" : ans;

solution/0400-0499/0402.Remove K Digits/Solution.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class Solution {
1414
}
1515
int i = 0;
1616
for (; i < stk.size() && stk[i] == '0'; ++i) {
17-
1817
}
1918
string ans = stk.substr(i);
2019
return ans == "" ? "0" : ans;

solution/0400-0499/0402.Remove K Digits/Solution.java

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public String removeKdigits(String num, int k) {
1313
}
1414
int i = 0;
1515
for (; i < stk.length() && stk.charAt(i) == '0'; ++i) {
16-
1716
}
1817
String ans = stk.substring(i);
1918
return "".equals(ans) ? "0" : ans;

solution/0700-0799/0712.Minimum ASCII Delete Sum for Two Strings/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ class Solution {
114114
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
115115
f[i][j] = f[i - 1][j - 1];
116116
} else {
117-
f[i][j] = Math.min(f[i - 1][j] + s1.charAt(i - 1), f[i][j - 1] + s2.charAt(j - 1));
117+
f[i][j]
118+
= Math.min(f[i - 1][j] + s1.charAt(i - 1), f[i][j - 1] + s2.charAt(j - 1));
118119
}
119120
}
120121
}

solution/0700-0799/0712.Minimum ASCII Delete Sum for Two Strings/README_EN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class Solution {
8383
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
8484
f[i][j] = f[i - 1][j - 1];
8585
} else {
86-
f[i][j] = Math.min(f[i - 1][j] + s1.charAt(i - 1), f[i][j - 1] + s2.charAt(j - 1));
86+
f[i][j]
87+
= Math.min(f[i - 1][j] + s1.charAt(i - 1), f[i][j - 1] + s2.charAt(j - 1));
8788
}
8889
}
8990
}

solution/0700-0799/0712.Minimum ASCII Delete Sum for Two Strings/Solution.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public int minimumDeleteSum(String s1, String s2) {
1313
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
1414
f[i][j] = f[i - 1][j - 1];
1515
} else {
16-
f[i][j] = Math.min(f[i - 1][j] + s1.charAt(i - 1), f[i][j - 1] + s2.charAt(j - 1));
16+
f[i][j]
17+
= Math.min(f[i - 1][j] + s1.charAt(i - 1), f[i][j - 1] + s2.charAt(j - 1));
1718
}
1819
}
1920
}

solution/0700-0799/0716.Max Stack/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ class Node {
167167
public Node prev, next;
168168

169169
public Node() {
170-
171170
}
172171

173172
public Node(int val) {
@@ -213,7 +212,6 @@ class MaxStack {
213212
private TreeMap<Integer, List<Node>> tm = new TreeMap<>();
214213

215214
public MaxStack() {
216-
217215
}
218216

219217
public void push(int x) {

solution/0700-0799/0716.Max Stack/README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ class Node {
139139
public Node prev, next;
140140

141141
public Node() {
142-
143142
}
144143

145144
public Node(int val) {
@@ -185,7 +184,6 @@ class MaxStack {
185184
private TreeMap<Integer, List<Node>> tm = new TreeMap<>();
186185

187186
public MaxStack() {
188-
189187
}
190188

191189
public void push(int x) {

solution/0700-0799/0716.Max Stack/Solution.java

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ class Node {
33
public Node prev, next;
44

55
public Node() {
6-
76
}
87

98
public Node(int val) {
@@ -49,7 +48,6 @@ class MaxStack {
4948
private TreeMap<Integer, List<Node>> tm = new TreeMap<>();
5049

5150
public MaxStack() {
52-
5351
}
5452

5553
public void push(int x) {

solution/0900-0999/0981.Time Based Key-Value Store/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ class TimeMap {
104104
private Map<String, TreeMap<Integer, String>> ktv = new HashMap<>();
105105

106106
public TimeMap() {
107-
108107
}
109108

110109
public void set(String key, String value, int timestamp) {

solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class TimeMap {
8181
private Map<String, TreeMap<Integer, String>> ktv = new HashMap<>();
8282

8383
public TimeMap() {
84-
8584
}
8685

8786
public void set(String key, String value, int timestamp) {

solution/0900-0999/0981.Time Based Key-Value Store/Solution.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class TimeMap {
22
public:
33
TimeMap() {
4-
54
}
6-
5+
76
void set(string key, string value, int timestamp) {
87
ktv[key].emplace_back(timestamp, value);
98
}
10-
9+
1110
string get(string key, int timestamp) {
1211
auto& pairs = ktv[key];
1312
pair<int, string> p = {timestamp, string({127})};

solution/0900-0999/0981.Time Based Key-Value Store/Solution.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ class TimeMap {
22
private Map<String, TreeMap<Integer, String>> ktv = new HashMap<>();
33

44
public TimeMap() {
5-
65
}
7-
6+
87
public void set(String key, String value, int timestamp) {
98
ktv.computeIfAbsent(key, k -> new TreeMap<>()).put(timestamp, value);
109
}
11-
10+
1211
public String get(String key, int timestamp) {
1312
if (!ktv.containsKey(key)) {
1413
return "";

solution/1000-1099/1020.Number of Enclaves/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public int numEnclaves(int[][] grid) {
2222
}
2323
return ans;
2424
}
25-
25+
2626
private void dfs(int i, int j) {
2727
grid[i][j] = 0;
2828
int[] dirs = {-1, 0, 1, 0, -1};

solution/1000-1099/1073.Adding Two Negabinary Numbers/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Solution {
120120
ans.add(x);
121121
}
122122
while (ans.size() > 1 && ans.get(ans.size() - 1) == 0) {
123-
ans.remove(ans.size() -1);
123+
ans.remove(ans.size() - 1);
124124
}
125125
Collections.reverse(ans);
126126
return ans.stream().mapToInt(x -> x).toArray();

solution/1000-1099/1073.Adding Two Negabinary Numbers/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Solution {
9595
ans.add(x);
9696
}
9797
while (ans.size() > 1 && ans.get(ans.size() - 1) == 0) {
98-
ans.remove(ans.size() -1);
98+
ans.remove(ans.size() - 1);
9999
}
100100
Collections.reverse(ans);
101101
return ans.stream().mapToInt(x -> x).toArray();

solution/1000-1099/1073.Adding Two Negabinary Numbers/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public int[] addNegabinary(int[] arr1, int[] arr2) {
1818
ans.add(x);
1919
}
2020
while (ans.size() > 1 && ans.get(ans.size() - 1) == 0) {
21-
ans.remove(ans.size() -1);
21+
ans.remove(ans.size() - 1);
2222
}
2323
Collections.reverse(ans);
2424
return ans.stream().mapToInt(x -> x).toArray();

solution/1400-1499/1490.Clone N-ary Tree/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ class Node {
44
public int val;
55
public List<Node> children;
66
7-
7+
88
public Node() {
99
children = new ArrayList<Node>();
1010
}
11-
11+
1212
public Node(int _val) {
1313
val = _val;
1414
children = new ArrayList<Node>();
1515
}
16-
16+
1717
public Node(int _val,ArrayList<Node> _children) {
1818
val = _val;
1919
children = _children;

solution/1500-1599/1510.Stone Game IV/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
private Boolean[] f;
3-
3+
44
public boolean winnerSquareGame(int n) {
55
f = new Boolean[n + 1];
66
return dfs(n);
77
}
8-
8+
99
private boolean dfs(int i) {
1010
if (i <= 0) {
1111
return false;

solution/1500-1599/1573.Number of Ways to Split a String/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Solution {
1717
cnt /= 3;
1818
auto find = [&](int x) {
1919
int t = 0;
20-
for (int i = 0; ; ++i) {
20+
for (int i = 0;; ++i) {
2121
t += s[i] == '1';
2222
if (t == x) {
2323
return i;

solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public:
149149
++i;
150150
t += up * boardingCost - runningCost;
151151
if (t > mx) {
152-
t = mx;
152+
mx = t;
153153
ans = i;
154154
}
155155
}

solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public:
132132
++i;
133133
t += up * boardingCost - runningCost;
134134
if (t > mx) {
135-
t = mx;
135+
mx = t;
136136
ans = i;
137137
}
138138
}

solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Solution {
1111
++i;
1212
t += up * boardingCost - runningCost;
1313
if (t > mx) {
14-
t = mx;
14+
mx = t;
1515
ans = i;
1616
}
1717
}

solution/1800-1899/1845.Seat Reservation Manager/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ class SeatManager {
55
q.push(i);
66
}
77
}
8-
8+
99
int reserve() {
1010
int seat = q.top();
1111
q.pop();
1212
return seat;
1313
}
14-
14+
1515
void unreserve(int seatNumber) {
1616
q.push(seatNumber);
1717
}

solution/1800-1899/1845.Seat Reservation Manager/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ public SeatManager(int n) {
66
q.offer(i);
77
}
88
}
9-
9+
1010
public int reserve() {
1111
return q.poll();
1212
}
13-
13+
1414
public void unreserve(int seatNumber) {
1515
q.offer(seatNumber);
1616
}

solution/1900-1999/1989.Maximum Number of People That Can Be Caught in Tag/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Solution {
88
while (j < n && (team[j] || i - j > dist)) {
99
++j;
1010
}
11-
if (j < n && abs(i - j) <= dist) {
11+
if (j < n && abs(i - j) <= dist) {
1212
++ans;
1313
++j;
1414
}

solution/2000-2099/2007.Find Original Array From Doubled Array/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Solution {
9797
public int[] findOriginalArray(int[] changed) {
9898
int n = changed.length;
9999
if (n % 2 == 1) {
100-
return new int[]{};
100+
return new int[] {};
101101
}
102102
Arrays.sort(changed);
103103
int[] cnt = new int[changed[n - 1] + 1];
@@ -111,13 +111,13 @@ class Solution {
111111
continue;
112112
}
113113
if (x * 2 >= cnt.length || cnt[x * 2] <= 0) {
114-
return new int[]{};
114+
return new int[] {};
115115
}
116116
ans[i++] = x;
117117
cnt[x]--;
118118
cnt[x * 2]--;
119119
}
120-
return i == n / 2 ? ans : new int[]{};
120+
return i == n / 2 ? ans : new int[] {};
121121
}
122122
}
123123
```

solution/2000-2099/2007.Find Original Array From Doubled Array/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Solution {
7878
public int[] findOriginalArray(int[] changed) {
7979
int n = changed.length;
8080
if (n % 2 == 1) {
81-
return new int[]{};
81+
return new int[] {};
8282
}
8383
Arrays.sort(changed);
8484
int[] cnt = new int[changed[n - 1] + 1];
@@ -92,13 +92,13 @@ class Solution {
9292
continue;
9393
}
9494
if (x * 2 >= cnt.length || cnt[x * 2] <= 0) {
95-
return new int[]{};
95+
return new int[] {};
9696
}
9797
ans[i++] = x;
9898
cnt[x]--;
9999
cnt[x * 2]--;
100100
}
101-
return i == n / 2 ? ans : new int[]{};
101+
return i == n / 2 ? ans : new int[] {};
102102
}
103103
}
104104
```

solution/2000-2099/2007.Find Original Array From Doubled Array/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public int[] findOriginalArray(int[] changed) {
33
int n = changed.length;
44
if (n % 2 == 1) {
5-
return new int[]{};
5+
return new int[] {};
66
}
77
Arrays.sort(changed);
88
int[] cnt = new int[changed[n - 1] + 1];
@@ -16,12 +16,12 @@ public int[] findOriginalArray(int[] changed) {
1616
continue;
1717
}
1818
if (x * 2 >= cnt.length || cnt[x * 2] <= 0) {
19-
return new int[]{};
19+
return new int[] {};
2020
}
2121
ans[i++] = x;
2222
cnt[x]--;
2323
cnt[x * 2]--;
2424
}
25-
return i == n / 2 ? ans : new int[]{};
25+
return i == n / 2 ? ans : new int[] {};
2626
}
2727
}

solution/2000-2099/2013.Detect Squares/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ class DetectSquares {
116116
private Map<Integer, Map<Integer, Integer>> cnt = new HashMap<>();
117117

118118
public DetectSquares() {
119-
120119
}
121120

122121
public void add(int[] point) {
@@ -136,8 +135,10 @@ class DetectSquares {
136135
int d = x2 - x1;
137136
var cnt1 = cnt.get(x1);
138137
var cnt2 = e.getValue();
139-
ans += cnt2.getOrDefault(y1, 0) * cnt1.getOrDefault(y1 + d, 0) * cnt2.getOrDefault(y1 + d, 0);
140-
ans += cnt2.getOrDefault(y1, 0) * cnt1.getOrDefault(y1 - d, 0) * cnt2.getOrDefault(y1 - d, 0);
138+
ans += cnt2.getOrDefault(y1, 0) * cnt1.getOrDefault(y1 + d, 0)
139+
* cnt2.getOrDefault(y1 + d, 0);
140+
ans += cnt2.getOrDefault(y1, 0) * cnt1.getOrDefault(y1 - d, 0)
141+
* cnt2.getOrDefault(y1 - d, 0);
141142
}
142143
}
143144
return ans;

0 commit comments

Comments
 (0)