696. 计数二进制子串

696. 计数二进制子串_第1张图片

 

696. 计数二进制子串_第2张图片

链接:https://leetcode-cn.com/problems/count-binary-substrings/solution/ji-shu-er-jin-zhi-zi-chuan-by-leetcode-solution/

代码:

class Solution {
public:
    int countBinarySubstrings(string s) {
        vector counts;
        int ptr = 0, n = s.size();
        while (ptr < n) {
            char c = s[ptr];
            int count = 0;
            while (ptr < n && s[ptr] == c) {
                ++ptr;
                ++count;
            }
            counts.push_back(count);
        }
        int ans = 0;
        for (int i = 1; i < counts.size(); ++i) {
            ans += min(counts[i], counts[i - 1]);
        }
        return ans;
    }
};

 

你可能感兴趣的:(leetcode)