1221. 分割平衡字符串
生活随笔
收集整理的這篇文章主要介紹了
1221. 分割平衡字符串
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2020-03-22
1.題目描述
分割平衡字符串2.題解
滿(mǎn)足平衡的條件就是L和R的個(gè)數(shù)相等,用一個(gè)計(jì)數(shù)器,L時(shí)+1,R時(shí)-1,為零時(shí)則滿(mǎn)足平衡字符串3.代碼
#include <iostream> #include <cstring> using namespace std;class Solution { public:int balancedStringSplit(string s) {int l=s.length(),cnt=0,res=0;for (int i=0;i<l;i++){if (s[i]=='L') cnt++;else cnt--;if (cnt==0){res++;}}return res;} };int main(){Solution s;cout<<s.balancedStringSplit("RLLLLRRRLR")<<endl;return 0; }總結(jié)
以上是生活随笔為你收集整理的1221. 分割平衡字符串的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HDU 1108.最小公倍数-辗转相除法
- 下一篇: php学习2