Skip to content

Commit

Permalink
921. 使括号有效的最少添加
Browse files Browse the repository at this point in the history
  • Loading branch information
BGMer7 committed Jul 29, 2022
1 parent 6b98c56 commit 348e1f2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/com/gatsby/_921MinimumAddToMakeParenthesesValid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.gatsby;

/**
* @ClassName: _921MinimumAddToMakeParenthesesValid
* @Description: 921. Minimum Add to Make Parentheses Valid
* @author: Gatsby
* @date: 2022/7/29 12:02
*/

public class _921MinimumAddToMakeParenthesesValid {
public int minAddToMakeValid(String s) {
int left = 0;
int count = 0;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == ')') {
if (left == 0) {
count++;
} else if (left > 0) {
left--;
}
} else {
left++;
}
}
return left + count;
}
}


0 comments on commit 348e1f2

Please sign in to comment.