Re: [闲聊] 每日leetcode

楼主: dont   2025-01-12 11:03:51
2116. Check if a Parentheses String Can Be Valid
## 思路
记录还没配对的括号个数 open
把locked==0的也当作 `(`
如果遇到配对不了的括号 就回传FALSE
两个方向各扫一遍
## Code
```cpp
class Solution {
public:
bool canBeValid(string s, string locked) {
int n = s.size();
if (n & 1) return false;
int open = 0;
for (int i=0; i<n; ++i) {
if (locked[i] == '0' || s[i] == '(') {
++open;
} else if (open == 0) {
return false;
} else {
作者: sustainer123 (caster)   2025-01-12 12:17:00
不用考虑AB喔?

Links booklink

Contact Us: admin [ a t ] ucptt.com