mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 1221.分割平衡字符串.md
added python and go solution to 1221
This commit is contained in:
@ -108,11 +108,38 @@ class Solution {
|
||||
### Python
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def balancedStringSplit(self, s: str) -> int:
|
||||
diff = 0 #右左差值
|
||||
ans = 0
|
||||
for c in s:
|
||||
if c == "L":
|
||||
diff -= 1
|
||||
else:
|
||||
diff += 1
|
||||
if tilt == 0:
|
||||
ans += 1
|
||||
return ans
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
```go
|
||||
func balancedStringSplit(s string) int {
|
||||
diff := 0 // 右左差值
|
||||
ans := 0
|
||||
for _, c := range s {
|
||||
if c == 'L' {
|
||||
diff--
|
||||
}else {
|
||||
diff++
|
||||
}
|
||||
if diff == 0 {
|
||||
ans++
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
```
|
||||
|
||||
### JavaScript
|
||||
|
Reference in New Issue
Block a user