diff --git a/problems/1221.分割平衡字符串.md b/problems/1221.分割平衡字符串.md index 08d4fee7..857df7ef 100644 --- a/problems/1221.分割平衡字符串.md +++ b/problems/1221.分割平衡字符串.md @@ -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