leetcode:1221. 分割平衡字符串

链接:https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/
记录字符串R与L的差值,为0时即可分割。
C++代码:

class Solution {
public:
    int balancedStringSplit(string s) {
        int temp = 0;
        int res = 0;
        for(int i = 0;i<s.size();i++)
        {
            if(s[i] == 'R')
                temp++;
            else 
                temp--;
            if(temp==0)
                res++;
        }
        return res;
    }
};

你可能感兴趣的:(leetcode,字符串,leetcode,算法)