LeetCode --- 贪心算法 --- 1221. 分割平衡字符串

这个思路很简单就是简单的计数。

深层次理解可以想成用计数方式代替栈

public class Solution {
    public int BalancedStringSplit(string s) {
        var maxCount = 0;
            var tmpCount = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i].Equals('R'))
                {
                    tmpCount++;
                }

                if (s[i].Equals('L'))
                {
                    tmpCount--;
                }

                if (tmpCount == 0)
                {
                    maxCount++;
                }
            }

            return maxCount;
    }
}

的功能

发布了71 篇原创文章 · 获赞 89 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/u012371712/article/details/104190587