-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1003 from 0xff-dev/1963
Add solution and test-cases for problem 1963
- Loading branch information
Showing
3 changed files
with
60 additions
and
22 deletions.
There are no files selected for viewing
43 changes: 30 additions & 13 deletions
43
...de/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 23 additions & 2 deletions
25
leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,26 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func Solution(s string) int { | ||
bs := []byte(s) | ||
stackIndex := -1 | ||
for index := 0; index < len(s); index++ { | ||
if s[index] == ']' { | ||
if stackIndex != -1 && bs[stackIndex] == '[' { | ||
stackIndex-- | ||
continue | ||
} | ||
} | ||
stackIndex++ | ||
bs[stackIndex] = s[index] | ||
} | ||
if stackIndex == -1 { | ||
return 0 | ||
} | ||
// 最终就是]]]]]][[[[ 这情况 | ||
count := (stackIndex + 1) / 2 | ||
need := count / 2 | ||
if count&1 == 1 { | ||
need++ | ||
} | ||
return need | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters