Skip to content

Commit 3e5c44c

Browse files
committed
Create 1541. 平衡括号字符串的最少插入次数.js
1 parent 89b4732 commit 3e5c44c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var minInsertions = function (s) {
6+
let result = 0;
7+
let left = 0;
8+
let right = 0;
9+
for (let i = 0; i < s.length; i++) {
10+
if (s[i] === '(') {
11+
left++;
12+
} else {
13+
if (left) {
14+
left--;
15+
} else {
16+
result++;
17+
}
18+
if (s[i + 1] === ')') {
19+
i++;
20+
} else {
21+
result++;
22+
}
23+
}
24+
}
25+
return result + left * 2;
26+
};

0 commit comments

Comments
 (0)