leetcode696 C++ 36ms 计算二进制子串


#include <iostream>
#include <vector>
#include <cmath>

class Solution {
public:
    int countBinarySubstrings(string s) {
        if(s.empty() || s.size() == 1){
            std::cout << 0 << std::endl;
        }

        std::vector<int> heap_vec;

        int heap = 1;
        for(int i = 0; i<s.size() - 1; i++){
            if(s[i] == s[i+1]){
                heap++;
            }
            else{
                heap_vec.push_back(heap);
                heap = 1;
            }
        }
        heap_vec.push_back(heap);

        int res=0;
        for(auto i = heap_vec.begin(); i < heap_vec.end() - 1; i++){
            res += min(*i, *(i+1));
        }

        return res;
    }
};

posted @ 2018-07-26 16:02  一条图图犬  阅读(250)  评论(0编辑  收藏  举报