1221.分隔平衡字符串

在这里插入图片描述

这题比较简单
贪心算法的运用,每步求出当前的最优子解

class Solution {
public:
    int balancedStringSplit(string s) {
        int n=0,count1=0,count2=0;
        for(auto c=s.begin();c!=s.end();++c)
        {
            if(*c=='R') ++count1;
            else ++count2;
            if(count1==count2)
            {
                count1=count2=0;
                ++n;
            }
        }
        return n;
    }
};

在这里插入图片描述

发布了90 篇原创文章 · 获赞 7 · 访问量 2183

猜你喜欢

转载自blog.csdn.net/weixin_43784305/article/details/102787467