From 195b8aefddc4266487c2c0894344134d4594bdb0 Mon Sep 17 00:00:00 2001 From: jerryfishcode <91447694+jerryfishcode@users.noreply.github.com> Date: Mon, 27 Sep 2021 17:23:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E1221.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=B9=B3=E8=A1=A1=E5=AD=97=E7=AC=A6=E4=B8=B2=20JavaScript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1221.分割平衡字符串.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/problems/1221.分割平衡字符串.md b/problems/1221.分割平衡字符串.md index c764e3ff..381ad060 100644 --- a/problems/1221.分割平衡字符串.md +++ b/problems/1221.分割平衡字符串.md @@ -108,6 +108,15 @@ public: ## JavaScript ```js +var balancedStringSplit = function(s) { + let res = 0, total = 0;//res为平衡字符串数量 total为当前"R"字符和"L"字符的数量差 + for(let c of s){// 遍历字符串每个字符 + //因为开始字符数量差就是0,遍历的时候要先改变数量差,否则会影响结果数量 + total += c === 'R' ? 1:-1;//遇到"R",total++;遇到"L",total-- + if(total === 0) res++;//只要"R""L"数量一样就可以算是一个平衡字符串 + } + return res; +}; ``` -----------------------