Skip to content

Commit 574cef4

Browse files
Update
1 parent 1609759 commit 574cef4

30 files changed

+1681
-231
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@
373373

374374
## 图论
375375

376-
通知:开始更新图论内容,图论部分还没有其他语言版本,欢迎录友们提交PR,成为contributor
376+
**[图论正式发布](./problems/qita/tulunfabu.md)**
377377

378378
1. [图论:理论基础](./problems/kamacoder/图论理论基础.md)
379379
2. [图论:深度优先搜索理论基础](./problems/kamacoder/图论深搜理论基础.md)

problems/0018.四数之和.md

+32-27
Original file line numberDiff line numberDiff line change
@@ -244,56 +244,61 @@ int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** return
244244
### Java:
245245
246246
```Java
247-
class Solution {
247+
import java.util.*;
248+
249+
public class Solution {
248250
public List<List<Integer>> fourSum(int[] nums, int target) {
249-
List<List<Integer>> result = new ArrayList<>();
250-
Arrays.sort(nums);
251-
252-
for (int i = 0; i < nums.length; i++) {
253-
254-
// nums[i] > target 直接返回, 剪枝操作
255-
if (nums[i] > 0 && nums[i] > target) {
256-
return result;
251+
Arrays.sort(nums); // 排序数组
252+
List<List<Integer>> result = new ArrayList<>(); // 结果集
253+
for (int k = 0; k < nums.length; k++) {
254+
// 剪枝处理
255+
if (nums[k] > target && nums[k] >= 0) {
256+
break;
257257
}
258-
259-
if (i > 0 && nums[i - 1] == nums[i]) { // 对nums[i]去重
258+
// 对nums[k]去重
259+
if (k > 0 && nums[k] == nums[k - 1]) {
260260
continue;
261261
}
262-
263-
for (int j = i + 1; j < nums.length; j++) {
264-
265-
// nums[i]+nums[j] > target 直接返回, 剪枝操作
266-
if (nums[i]+nums[j] > 0 && nums[i]+nums[j] > target) {
267-
return result;
268-
}
269-
270-
if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重
262+
for (int i = k + 1; i < nums.length; i++) {
263+
// 第二级剪枝
264+
if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) {
265+
break;
266+
}
267+
// 对nums[i]去重
268+
if (i > k + 1 && nums[i] == nums[i - 1]) {
271269
continue;
272270
}
273-
274-
int left = j + 1;
271+
int left = i + 1;
275272
int right = nums.length - 1;
276273
while (right > left) {
277-
// nums[k] + nums[i] + nums[left] + nums[right] > target int会溢出
278-
long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
274+
long sum = (long) nums[k] + nums[i] + nums[left] + nums[right];
279275
if (sum > target) {
280276
right--;
281277
} else if (sum < target) {
282278
left++;
283279
} else {
284-
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
280+
result.add(Arrays.asList(nums[k], nums[i], nums[left], nums[right]));
285281
// 对nums[left]和nums[right]去重
286282
while (right > left && nums[right] == nums[right - 1]) right--;
287283
while (right > left && nums[left] == nums[left + 1]) left++;
288-
289-
left++;
290284
right--;
285+
left++;
291286
}
292287
}
293288
}
294289
}
295290
return result;
296291
}
292+
293+
public static void main(String[] args) {
294+
Solution solution = new Solution();
295+
int[] nums = {1, 0, -1, 0, -2, 2};
296+
int target = 0;
297+
List<List<Integer>> results = solution.fourSum(nums, target);
298+
for (List<Integer> result : results) {
299+
System.out.println(result);
300+
}
301+
}
297302
}
298303
```
299304

problems/0084.柱状图中最大的矩形.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ public:
188188

189189
开头为什么要加元素0?
190190

191-
如果数组本身是降序的,例如 [8,6,4,2],在 8 入栈后,6 开始与8 进行比较,此时我们得到 mid(8),rigt(6),但是得不到 left。
191+
如果数组本身是降序的,例如 [8,6,4,2],在 8 入栈后,6 开始与8 进行比较,此时我们得到 mid(8),right(6),但是得不到 left。
192192

193193
(mid、left,right 都是对应版本一里的逻辑)
194194

195195
因为 将 8 弹出之后,栈里没有元素了,那么为了避免空栈取值,直接跳过了计算结果的逻辑。
196196

197-
之后又将6 加入栈(此时8已经弹出了),然后 就是 4 与 栈口元素 8 进行比较,周而复始,那么计算的最后结果resutl就是0。 如图所示:
197+
之后又将6 加入栈(此时8已经弹出了),然后 就是 4 与 栈口元素 6 进行比较,周而复始,那么计算的最后结果result就是0。 如图所示:
198198

199199
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221164533.png)
200200

problems/0139.单词拆分.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public:
243243

244244
使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下:
245245

246-
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221123205105.png)
246+
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240809155103.png)
247247

248248
最后dp[s.size()] = 0 即 dp[13] = 0 ,而不是1,因为先用 "apple" 去遍历的时候,dp[8]并没有被赋值为1 (还没用"pen"),所以 dp[13]也不能变成1。
249249

problems/0225.用队列实现栈.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public:
131131
}
132132
};
133133
```
134-
* 时间复杂度: pop为O(n),其他为O(1)
134+
* 时间复杂度: pop为O(n),top为O(n),其他为O(1)
135135
* 空间复杂度: O(n)
136136
137137
## 优化
@@ -147,17 +147,14 @@ class MyStack {
147147
public:
148148
queue<int> que;
149149
150-
/** Initialize your data structure here. */
151150
MyStack() {
152151
153152
}
154153
155-
/** Push element x onto stack. */
156154
void push(int x) {
157155
que.push(x);
158156
}
159157
160-
/** Removes the element on top of the stack and returns that element. */
161158
int pop() {
162159
int size = que.size();
163160
size--;
@@ -170,9 +167,6 @@ public:
170167
return result;
171168
}
172169
173-
/** Get the top element.
174-
** Can not use back() direactly.
175-
*/
176170
int top(){
177171
int size = que.size();
178172
size--;
@@ -187,13 +181,12 @@ public:
187181
return result;
188182
}
189183
190-
/** Returns whether the stack is empty. */
191184
bool empty() {
192185
return que.empty();
193186
}
194187
};
195188
```
196-
* 时间复杂度: pop为O(n),其他为O(1)
189+
* 时间复杂度: pop为O(n),top为O(n),其他为O(1)
197190
* 空间复杂度: O(n)
198191

199192

0 commit comments

Comments
 (0)