From e391bf18f7f0c607a918413d26866a9f376ced1e Mon Sep 17 00:00:00 2001 From: van_fantasy <46948123+sexyxlyWAol@users.noreply.github.com> Date: Mon, 13 Jun 2022 22:44:49 +0800 Subject: [PATCH] =?UTF-8?q?Update=201221.=E5=88=86=E5=89=B2=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit added python and go solution to 1221 --- problems/1221.分割平衡字符串.md | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/1221.分割平衡字符串.md b/problems/1221.分割平衡字符串.md index 1a9b34a2..e18c6358 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