1221 分割平衡字符串

输入字符串中L和R数目已经相同
题目未要求求各平衡字符串,若要求出各平衡字符串则应用双指针遍历或者栈

class Solution {
public:
    int balancedStringSplit(string s) {
        int ans = 0, cnt = 0;
        for(int i = 0; i < s.size(); ++i)
        {
            if(s[i] == 'L') ++cnt;
            else --cnt;
            if(!cnt) ++ans;
        }
        return ans ? ans : 1;
    }
};
发布了21 篇原创文章 · 获赞 32 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/include_not_found_/article/details/104896378
今日推荐