美文网首页
1221. 分割平衡字符串

1221. 分割平衡字符串

作者: 漫行者_ | 来源:发表于2021-09-08 23:58 被阅读0次

1221. 分割平衡字符串

本题的贪心算法,只要l和r完整就算一个。

    public int balancedStringSplit(String s) {
        if(null == s || s.length()  == 0) return 0;
        int nums = 0;
        int count = 0;
        for(int i=0; i<s.length(); i++) {
            if(s.charAt(i) == 'L') {
                count++;
            } else {
                count--;
            }
            if(count == 0) {
                nums++;
            }
        }
        return nums;
    }

相关文章

网友评论

      本文标题:1221. 分割平衡字符串

      本文链接:https://www.haomeiwen.com/subject/rctmwltx.html