Count Binary Substrings

class Solution {
public:
    int countBinarySubstrings(string s) {
        vector<int> res;
        int tol = 1;
        for (int i=1; i<=s.length(); i++) {
            if (s[i]==s[i-1]) {
                tol++;
            }else{
                res.push_back(tol);
                tol =1;
            }
        }
        int count = 0;
        for(int i=1, n=res.size(); i<n; ++i){
            count += min(res[i-1], res[i]);
        }
        return count;
    }
};
2017/12/4 posted in  leetcode

Detect Capital

class Solution {
public:
    bool detectCapitalUse(string word) {
        int a[word.length()];
        int r = 0;
        for (int i = 0; i<word.length(); i++) {
            if (word[i]>='A'&&word[i]<='Z') {
                a[i] = 1;
                r+=a[i];
            }
        }
        if (r==word.length()||(a[0]==1&&r==1)||r==0) {
            return true;
        }
        return false;
    }
};
2017/12/4 posted in  leetcode

Reverse String

class Solution {
public:
    string reverseString(string s) {
        int str = 0;
        int end = s.length()-1;
        while (str<=end) {
            char tmp;
            tmp = s[str];
            s[str] = s[end];
            s[end] = tmp;
            str++;
            end--;
        }
        return s;
    }
};
2017/12/4 posted in  leetcode

Reverse Words in a String III

class Solution {
public:
    string reverseWords(string s) {
        string res;
        int str = 0;
        for (int i=0; i<s.length(); i++) {
            if (s[i]==' '||i==s.length()-1) {
                int j = i==s.length()-1 ? i:i-1;
                while (j>=str) {
                    char tmp = s[str];
                    s[str] = s[j];
                    s[j] = tmp;
                    j--;
                    str++;
                }
                str = i+1;
            }
        }

        return s;
    }
};
2017/12/4 posted in  leetcode

Judge Route Circle

class Solution {
public:
    bool judgeCircle(string moves) {
        int UD = 0 ,LR = 0;
        for(char a:moves){
            if (a=='U') {
                UD++;
            }else if (a=='D'){
                UD--;
            }else if (a=='L'){
                LR++;
            }else if (a=='R'){
                LR--;
            }
        }
        return (UD==0&&LR==0) ? 1:0;
    }
};
2017/12/2 posted in  leetcode

Self Dividing Numbers

class Solution {
public:
    vector<int> selfDividingNumbers(int left, int right) {
        vector<int> v;
        for (int i = left; i <= right; ++i) {
            int j= i;
            bool ok = 1;
            while (j) {
                int d= (j%10);
                if (d == 0 || i % d) ok = 0;
                j/=10;
            }
            if (ok) v.push_back(i);
        }
        return v;
    }
};
2017/12/2 posted in  leetcode