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

Commit25804e5

Browse files
add 2116
1 parentb83816d commit25804e5

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2116|[Check if a Parentheses String Can Be Valid](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2116.java)||Medium||
1112
|2114|[Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2114.java)||Easy||
1213
|2110|[Number of Smooth Descent Periods of a Stock](https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2110.java)||Medium||
1314
|2109|[Adding Spaces to a String](https://leetcode.com/problems/adding-spaces-to-a-string/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2109.java)||Medium||
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_2116 {
4+
publicstaticclassSolution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/discuss/1646594/Left-to-right-and-right-to-left
7+
*/
8+
publicbooleancanBeValid(Strings,Stringlocked) {
9+
returns.length() %2 ==0 &&valid(s,locked,'(') &&valid(s,locked,')');
10+
}
11+
12+
privatebooleanvalid(Strings,Stringlocked,charop) {
13+
intbalance =0;
14+
intwildcards =0;
15+
intdirection =op =='(' ?1 : -1;
16+
intstart =op =='(' ?0 :s.length() -1;
17+
for (inti =start;i <s.length() &&i >=0 &&balance +wildcards >=0;i +=direction) {
18+
if (locked.charAt(i) =='1') {
19+
balance +=s.charAt(i) ==op ?1 : -1;
20+
}else {
21+
wildcards++;
22+
}
23+
}
24+
returnwildcards +balance >=0 &&balance <=wildcards;
25+
}
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._2116;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_2116Test {
10+
privatestatic_2116.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_2116.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(true,solution1.canBeValid("))()))","010100"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(true,solution1.canBeValid("()()","0000"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(false,solution1.canBeValid(")","0"));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(true,solution1.canBeValid(")(","00"));
35+
}
36+
37+
@Test
38+
publicvoidtest5() {
39+
assertEquals(false,solution1.canBeValid("())(()(()(())()())(())((())(()())((())))))(((((((())(()))))(","100011110110011011010111100111011101111110000101001101001111"));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp