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

Commit 6a55e09

Browse files
Merge pull request youngyangyang04#2867 from JianWangUCD/jian
更新20有效的括号 Java更简单易懂的新解法、18四数之和 修改思路第二段末尾描述、修改239滑动窗口最大值java代码注释严重错误
2 parents 9f203fb + 588e42c commit 6a55e09

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

‎problems/0018.四数之和.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。
3737

38-
但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target``-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[i] > target && (nums[i] >=0 || target >= 0)`就可以了。
38+
但是有一些细节需要注意,例如: 不要判断`nums[k] > target` 就返回了,三数之和 可以通过 `nums[i] > 0` 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是`[-4, -3, -2, -1]`,`target``-10`,不能因为`-4 > -10`而跳过。但是我们依旧可以去做剪枝,逻辑变成`nums[k] > target && (nums[k] >=0 || target >= 0)`就可以了。
3939

4040
[15.三数之和](https://programmercarl.com/0015.三数之和.html)的双指针解法是一层for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0。
4141

@@ -802,3 +802,4 @@ end
802802
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
803803
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
804804
</a>
805+

‎problems/0020.有效的括号.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,29 @@ class Solution {
172172
}
173173
```
174174

175+
```java
176+
// 解法二
177+
// 对应的另一半一定在栈顶
178+
class Solution {
179+
public boolean isValid(String s) {
180+
Stack<Character> stack = new Stack<>();
181+
for(char c : s.toCharArray()){
182+
// 有对应的另一半就直接消消乐
183+
if(c == ')' && !stack.isEmpty() && stack.peek() == '(')
184+
stack.pop();
185+
else if(c == '}' && !stack.isEmpty() && stack.peek() == '{')
186+
stack.pop();
187+
else if(c == ']' && !stack.isEmpty() && stack.peek() == '[')
188+
stack.pop();
189+
else
190+
stack.push(c);// 没有匹配的就放进去
191+
}
192+
193+
return stack.isEmpty();
194+
}
195+
}
196+
```
197+
175198
### Python:
176199

177200
```python

‎problems/0239.滑动窗口最大值.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class Solution {
267267
//利用双端队列手动实现单调队列
268268
/**
269269
* 用一个单调队列来存储对应的下标,每当窗口滑动的时候,直接取队列的头部指针对应的值放入结果集即可
270-
* 单调队列类似 (tail -->) 3 --> 2 --> 1 --> 0 (--> head) (右边为头结点,元素存的是下标)
270+
* 单调递减队列类似 (head -->) 3 --> 2 --> 1 --> 0 (--> tail) (左边为头结点,元素存的是下标)
271271
*/
272272
class Solution {
273273
public int[] maxSlidingWindow(int[] nums, int k) {
@@ -281,7 +281,7 @@ class Solution {
281281
while(!deque.isEmpty() && deque.peek() < i - k + 1){
282282
deque.poll();
283283
}
284-
// 2.既然是单调,就要保证每次放进去的数字要比末尾的都大,否则也弹出
284+
// 2.维护单调递减队列:新元素若大于队尾元素,则弹出队尾元素,直到满足单调性
285285
while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
286286
deque.pollLast();
287287
}
@@ -926,3 +926,4 @@ int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
926926
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
927927
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
928928
</a>
929+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /