【leetcode】696. 计数二进制子串

 

#define min(a,b) (a<b)?(a) :(b)
int countBinarySubstrings(char * s){
    int i, j, cnt = 0, len = strlen(s), sum=0, preVal=0, ret;
    for (i = 0; i < len; i++){
        cnt++;
        if (i == len-1 || s[i] != s[i + 1]){
            sum += min(cnt, preVal);
            preVal = cnt;
            cnt = 0;
        }
    }
    return sum;
}

 

posted @ 2020-11-24 20:11  温暖了寂寞  阅读(58)  评论(0编辑  收藏  举报