新增1221.分割平衡字符串 JavaScript版本

This commit is contained in:
jerryfishcode
2021-09-27 17:23:26 +08:00
committed by GitHub
parent a1e32680ac
commit 195b8aefdd

View File

@ -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;
};
```
-----------------------