Skip to content

Commit 67cf682

Browse files
authored
update: 添加问题“2116.判断一个括号字符串是否有效”的代码和题解(#833)
* v3: WA Signed-off-by: LetMeFly666 <[email protected]> * AC: 2116.cpp 先休息会儿 久坐了有点 Signed-off-by: LetMeFly666 <[email protected]> * solve: (#832) - under verify ewSolution.py创建新分支后,直接先与推送到远端并绑定 Signed-off-by: LetMeFly666 <[email protected]> * AC: 2116.py Signed-off-by: LetMeFly666 <[email protected]> * 编译不通过: 2116.java Signed-off-by: LetMeFly666 <[email protected]> * AC: 2116.java Signed-off-by: LetMeFly666 <[email protected]> * AC: 2116.go Signed-off-by: LetMeFly666 <[email protected]> * words: en&jp words Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“2116.判断一个括号字符串是否有效”的代码和题解(#833) solve: (#832) - under verify `newSolution.py`创建新分支后,直接先与推送到远端并绑定 words: en&jp words Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 0fa88cc commit 67cf682

10 files changed

+516
-11
lines changed

.commitmsg

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
docs: en&jp words(2 days)
1+
solve: (#832) - under verify
2+
`newSolution.py`创建新分支后,直接先与推送到远端并绑定
23

3-
codes: 美团笔试 - WA版本
4-
美团笔试最后思路想出来了但是没实现出来
5-
6-
docs: 美团笔试 - 题解(Half版)
7-
8-
backup: 防止脚本清除此行
9-
|牛客-美团暑期2025-20250322-前两题和第三题的思路|<a href="https://blog.letmefly.xyz/2025/03/22/Nowcoder-MeiTuan-Summer2025-20250322/">本平台题解</a>|<a href="https://letmefly.blog.csdn.net/article/details/146448313">CSDN题解</a>|
10-
11-
close: #829
4+
words: en&jp words
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-23 09:37:28
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-23 10:39:28
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/*
12+
*)*(**
13+
()(())
14+
15+
16+
(*)*
17+
(())
18+
19+
20+
(**(
21+
xxxx
22+
23+
(*
24+
()
25+
26+
27+
28+
*/
29+
// class Solution {
30+
// public:
31+
// bool canBeValid(string s, string locked) {
32+
// int custome = 0, needRight;
33+
// for (int i = 0; i < locked.size(); i++) {
34+
// if (locked[i] == '0') {
35+
// custome++;
36+
// } else if (s[i] == ')') {
37+
// if (!custome && !needRight) {
38+
// return false;
39+
// }
40+
// if (needRight) {
41+
// needRight--;
42+
// } else {
43+
// custome--;
44+
// }
45+
// } else { // s[i] is '(' and locked
46+
// needRight++;
47+
// }
48+
// }
49+
// return custome
50+
// }
51+
// };
52+
53+
/*
54+
custome: 当前有多少个没被预约的*
55+
needRight: 还需要多少个)
56+
如果遇到)就直接消耗一个needRight
57+
如果遇到*就先累积起来到reserved,看会不会碰到)
58+
*/
59+
// class Solution {
60+
// public:
61+
// bool canBeValid(string s, string locked) {
62+
// int custome = 0, needRight = 0, reserved = 0;
63+
// for (int i = 0; i < locked.size(); i++) {
64+
// if (locked[i] == '0') {
65+
// if (needRight) {
66+
// needRight--;
67+
// reserved++;
68+
// } else {
69+
// custome++;
70+
// }
71+
// } else if (s[i] == ')') {
72+
// if (needRight) {
73+
// needRight--;
74+
// } else if ()
75+
// } else {
76+
77+
// }
78+
// }
79+
// }
80+
// };
81+
82+
class Solution {
83+
public:
84+
bool canBeValid(string s, string locked) {
85+
int l = 0, r = 0;
86+
for (int i = 0; i < s.size(); i++) {
87+
if (locked[i] == '0') {
88+
l--, r++;
89+
if (l < 0) {
90+
l = 1;
91+
}
92+
} else { // ()
93+
if (s[i] == '(') {
94+
l++, r++;
95+
} else {
96+
l--, r--;
97+
if (r < 0) {
98+
return false;
99+
}
100+
if (l < 0) {
101+
l = 1;
102+
}
103+
}
104+
}
105+
}
106+
return !l;
107+
}
108+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-23 10:54:53
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-23 10:56:04
6+
*/
7+
package main
8+
9+
func canBeValid(s string, locked string) bool {
10+
l, r := 0, 0
11+
for i := range s {
12+
if locked[i] == '0' {
13+
l--
14+
r++
15+
if l < 0 {
16+
l = 1
17+
}
18+
} else {
19+
if s[i] == '(' {
20+
l++
21+
r++
22+
} else {
23+
l--
24+
r--
25+
if r < 0 {
26+
return false
27+
}
28+
if l < 0 {
29+
l = 1
30+
}
31+
}
32+
}
33+
}
34+
return l == 0
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-23 10:52:22
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-23 10:53:49
6+
*/
7+
class Solution {
8+
public boolean canBeValid(String s, String locked) {
9+
int l = 0, r = 0;
10+
for (int i = 0; i < s.length(); i++) {
11+
if (locked.charAt(i) == '0') {
12+
l--;
13+
r++;
14+
if (l < 0) {
15+
l = 1;
16+
}
17+
} else {
18+
if (s.charAt(i) == '(') {
19+
l++;
20+
r++;
21+
} else {
22+
l--;
23+
r--;
24+
if (r < 0) {
25+
return false;
26+
}
27+
if (l < 0) {
28+
l = 1;
29+
}
30+
}
31+
}
32+
}
33+
return l == 0;
34+
}
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-23 10:49:44
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-23 10:51:19
6+
'''
7+
class Solution:
8+
def canBeValid(self, s: str, locked: str) -> bool:
9+
l = r = 0
10+
for i in range(len(s)):
11+
if locked[i] == '0':
12+
l -= 1
13+
r += 1
14+
if l == -1:
15+
l = 1
16+
else:
17+
if s[i] == '(':
18+
l += 1
19+
r += 1
20+
else:
21+
l -= 1
22+
r -= 1
23+
if r < 0:
24+
return False
25+
if l < 0:
26+
l = 1
27+
return not l

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@
658658
|2101.引爆最多的炸弹|中等|<a href="https://leetcode.cn/problems/detonate-the-maximum-bombs/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/23/LeetCode%202101.%E5%BC%95%E7%88%86%E6%9C%80%E5%A4%9A%E7%9A%84%E7%82%B8%E5%BC%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140629296" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/detonate-the-maximum-bombs/solutions/2854053/letmefly-2101yin-bao-zui-duo-de-zha-dan-m73kr/" target="_blank">LeetCode题解</a>|
659659
|2105.给植物浇水II|中等|<a href="https://leetcode.cn/problems/watering-plants-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/05/LeetCode%202105.%E7%BB%99%E6%A4%8D%E7%89%A9%E6%B5%87%E6%B0%B4II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126709258" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/watering-plants-ii/solution/by-tisfy-0erl/" target="_blank">LeetCode题解</a>|
660660
|2106.摘水果|困难|<a href="https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/04/LeetCode%202106.%E6%91%98%E6%B0%B4%E6%9E%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130482457" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/solutions/2255124/letmefly-2106zhai-shui-guo-by-tisfy-tb8v/" target="_blank">LeetCode题解</a>|
661+
|2116.判断一个括号字符串是否有效|中等|<a href="https://leetcode.cn/problems/check-if-a-parentheses-string-can-be-valid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/23/LeetCode%202116.%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%8B%AC%E5%8F%B7%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E6%9C%89%E6%95%88/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146454056" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-a-parentheses-string-can-be-valid/solutions/3624225/letmefly-2116pan-duan-yi-ge-gua-hao-zi-f-9n2z/" target="_blank">LeetCode题解</a>|
661662
|2129.将标题首字母大写|简单|<a href="https://leetcode.cn/problems/capitalize-the-title/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/11/LeetCode%202129.%E5%B0%86%E6%A0%87%E9%A2%98%E9%A6%96%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%86%99/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136614914" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/capitalize-the-title/solutions/2679708/letmefly-2129jiang-biao-ti-shou-zi-mu-da-nj90/" target="_blank">LeetCode题解</a>|
662663
|2132.用邮票贴满网格图|困难|<a href="https://leetcode.cn/problems/stamping-the-grid/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/14/LeetCode%202132.%E7%94%A8%E9%82%AE%E7%A5%A8%E8%B4%B4%E6%BB%A1%E7%BD%91%E6%A0%BC%E5%9B%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135002925" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/stamping-the-grid/solutions/2566644/letmefly-2132yong-you-piao-tie-man-wang-znigh/" target="_blank">LeetCode题解</a>|
663664
|2171.拿出最少数目的魔法豆|中等|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/18/LeetCode%202171.%E6%8B%BF%E5%87%BA%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E9%AD%94%E6%B3%95%E8%B1%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135682601" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/solutions/2609831/letmefly-2171na-chu-zui-shao-shu-mu-de-m-jima/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)