Update 1221.分割平衡字符串.md

added python and go solution to 1221
This commit is contained in:
van_fantasy
2022-06-13 22:44:49 +08:00
committed by GitHub
parent 2c1aa6ebf8
commit e391bf18f7

View File

@ -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