只有满足下面几点之一,括号字符串才是有效的:
它是一个空字符串,或者
它可以被写成 AB (A 与 B 连接), 其中 A 和 B 都是有效字符串,或者
它可以被写作 (A),其中 A 是有效字符串。
给定一个括号字符串 s ,移动N次,你就可以在字符串的任何位置插入一个括号。
例如,如果 s = "()))" ,你可以插入一个开始括号为 "(()))" 或结束括号为 "())))" 。
返回 为使结果字符串 s 有效而必须添加的最少括号数。
示例 1:
输入:s = "())"
输出:1
示例 2:
输入:s = "((("
输出:3
提示:
1 <= s.length <= 1000
s 只包含 '(' 和 ')' 字符。
var minAddToMakeValid = function(s) {
const n = s.length
let cnt0 = cnt1 = r = 0
for (let i = 0; i < n; i++) {
if (s[i] === '(') {
if (cnt1 > cnt0) {
r += cnt1 - cnt0
cnt0 = cnt1 = 0
}
cnt0++
} else cnt1++
}
return r + Math.abs(cnt1 - cnt0)
};
function minAddToMakeValid(s: string): number {
const n = s.length
let cnt0 = 0, cnt1 = 0, r = 0
for (let i = 0; i < n; i++) {
if (s[i] === '(') {
if (cnt1 > cnt0) {
r += cnt1 - cnt0
cnt1 = 0
cnt0 = 0
}
cnt0++
} else cnt1++
}
return r + Math.abs(cnt1 - cnt0)
};
class Solution {
function minAddToMakeValid($s) {
$n = strlen($s);
$r = $cnt0 = $cnt1 = 0;
for ($i = 0; $i < $n; $i++) {
if ($s[$i] === '(') {
if ($cnt1 > $cnt0) {
$r += $cnt1 - $cnt0;
$cnt1 = $cnt0 = 0;
}
$cnt0++;
} else $cnt1++;
}
return $r + abs($cnt1 - $cnt0);
}
}
func minAddToMakeValid(s string) int {
cnt0, cnt1, r := 0, 0, 0
for _, c := range s {
if c == '(' {
if cnt1 > cnt0 {
r += cnt1 - cnt0
cnt0, cnt1 = 0, 0
}
cnt0++
} else {
cnt1++
}
}
t := cnt1 - cnt0
return r + (t ^ t >> 31 - t >> 31)
}
class Solution {
public int minAddToMakeValid(String s) {
int cnt0 = 0, cnt1 = 0, r = 0;
for (char c : s.toCharArray()) {
if (c == '(') {
if (cnt1 > cnt0) {
r += cnt1 - cnt0;
cnt1 = cnt0 = 0;
}
cnt0++;
} else cnt1++;
}
int t = cnt1 - cnt0;
return r + (t ^ t >> 31) - (t >> 31);
}
}
public class Solution {
public int MinAddToMakeValid(string s) {
int cnt0 = 0, cnt1 = 0, r = 0;
foreach(char c in s) {
if (c == '(') {
if (cnt1 > cnt0) {
r+= cnt1 - cnt0;
cnt1 = cnt0 = 0;
}
cnt0++;
} else cnt1++;
}
return r + Math.Abs(cnt0 - cnt1);
}
}
int minAddToMakeValid(char * s){
int n = strlen(s), cnt0 = 0, cnt1 = 0, r = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
if (cnt1 > cnt0) {
r += cnt1 - cnt0;
cnt0 = cnt1 = 0;
}
cnt0++;
} else cnt1++;
}
return r + abs(cnt1 - cnt0);
}
class Solution {
public:
int minAddToMakeValid(string s) {
int cnt0 = 0, cnt1 = 0, r = 0;
for (char c : s) {
if (c == '(') {
if (cnt1 > cnt0) {
r += cnt1 - cnt0;
cnt1 = cnt0 = 0;
}
cnt0++;
}
else cnt1++;
}
return r + abs(cnt1 - cnt0);
}
};
class Solution:
def minAddToMakeValid(self, s: str) -> int:
cnt0, cnt1, r = 0, 0, 0
for c in s:
if c == '(':
if cnt1 > cnt0:
r += cnt1 - cnt0
cnt0, cnt1 = 0, 0
cnt0 += 1
else: cnt1 += 1
return r + abs(cnt1 - cnt0)
一个变量统计左括号数量 遇到 右括号 时,有 左括号,减少左括号数量(匹配左括号),没有左括号,则填括号(结果变量 + 1)
var minAddToMakeValid = function(s) {
const n = s.length
let cnt = r = 0
for (let i = 0; i < n; i++) {
if (s[i] === '(') cnt++
else if (cnt) cnt--
else r++
}
return r + cnt
};
function minAddToMakeValid(s: string): number {
const n = s.length
let cnt = 0, r = 0
for (let i = 0; i < n; i++) {
if (s[i] === '(') cnt++
else if (cnt) cnt--
else r++
}
return r + cnt
};
class Solution {
function minAddToMakeValid($s) {
$n = strlen($s);
$r = $cnt = 0;
for ($i = 0; $i < $n; $i++) {
if ($s[$i] === '(') $cnt++;
elseif ($cnt) $cnt--;
else $r++;
}
return $r + $cnt;
}
}
func minAddToMakeValid(s string) int {
cnt, r := 0, 0
for _, c := range s {
if c == '(' {
cnt++
} else if cnt > 0 {
cnt--
} else {
r++
}
}
return r + cnt
}
class Solution {
public int minAddToMakeValid(String s) {
int n = s.length(), cnt = 0, r = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '(') cnt++;
else if (cnt > 0) cnt--;
else r++;
}
return r + cnt;
}
}
public class Solution {
public int MinAddToMakeValid(string s) {
int n = s.Length, cnt = 0, r = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') cnt++;
else if (cnt > 0) cnt--;
else r++;
}
return r + cnt;
}
}
int minAddToMakeValid(char * s){
int n = strlen(s), cnt = 0, r = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') cnt++;
else if (cnt) cnt--;
else r++;
}
return r + cnt;
}
class Solution {
public:
int minAddToMakeValid(string s) {
int cnt = 0, r = 0;
for (char c : s) {
if (c == '(') cnt++;
else if (cnt) cnt--;
else r++;
}
return r + cnt;
}
};
class Solution:
def minAddToMakeValid(self, s: str) -> int:
cnt, r = 0, 0
for c in s:
if c == '(': cnt += 1
elif cnt > 0: cnt -= 1
else: r += 1
return r + cnt