Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit38291e5

Browse files
add 1249
1 parentc3de63f commit38291e5

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ _If you like this project, please leave me a star._ ★
244244
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java)|[:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
245245
|1258|[Synonymous Sentences](https://leetcode.com/problems/synonymous-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1258.java)||Medium|Backtracking|
246246
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java)||Easy||
247-
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java)||Easy||
247+
|1249|[Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1249.java)||Medium|String, Stack|
248248
|1243|[Array Transformation](https://leetcode.com/problems/array-transformation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1243.java)|[:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs)|Easy||
249+
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java)||Easy||
249250
|1232|[Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1232.java)|[:tv:](https://www.youtube.com/watch?v=_tfiTQNZCbs)|Easy||
250251
|1228|[Missing Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1228.java)||Easy||
251252
|1221|[Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1221.java)||Easy|Greedy|
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
packagecom.fishercoder.solutions;
22

3+
importjava.util.Stack;
4+
35
publicclass_1249 {
46
publicstaticclassSolution1 {
57
publicStringminRemoveToMakeValid(Strings) {
6-
//TODO: implement it
7-
return"";
8+
Stack<Character>stack =newStack<>();
9+
intleftParen =0;
10+
intrightParen =0;
11+
for (charc :s.toCharArray()) {
12+
if (c =='(') {
13+
stack.push(c);
14+
leftParen++;
15+
}elseif (c ==')') {
16+
if (leftParen >rightParen) {
17+
stack.push(c);
18+
rightParen++;
19+
}
20+
}else {
21+
stack.push(c);
22+
}
23+
}
24+
StringBuildersb =newStringBuilder();
25+
intdiff =leftParen -rightParen;
26+
while (!stack.isEmpty()) {
27+
if (stack.peek() =='(') {
28+
if (diff-- >0) {
29+
stack.pop();
30+
}else {
31+
sb.append(stack.pop());
32+
}
33+
}else {
34+
sb.append(stack.pop());
35+
}
36+
}
37+
returnsb.reverse().toString();
838
}
939
}
1040
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp