Implement strStr()

class Solution {
public:
    int strStr(string haystack, string needle) {
        int j = 0;  //标记needle的初始位置
        int h = haystack.length(), n = needle.length();
        if (!n) {
            return 0;
        }
        for (int i =0; i<h-n+1; i++) {
            int tmp = i;
            while (tmp<h && j<n && haystack[tmp]==needle[j]) {
                tmp++;
                j++;
            }
            if (j==n) {
                return i;
            }else{
                j=0;
            }
        }
        return -1;
    }
};
2017/12/4 posted in  leetcode

String Compression

Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.

Follow up:
Could you solve it using only O(1) extra space?

class Solution {
public:
    int compress(vector<char>& chars) {
        int tol = 1;    //统计相同字符的数量
        for (int i = 0; i<chars.size(); i++) {
            while (i!=chars.size()-1 && chars[i]==chars[i+1]) {
                chars.erase(chars.begin()+i+1);
                tol++;
            }
            int log = 0; //标记插入数字是1的情况
            int logNum = 0;  //统计插入的数字个数
            while (log==1||tol!=1 && tol>0) {
                chars.insert(chars.begin()+i+1, char(tol%10+'0'));
                tol/=10;
                log = tol==1?tol:0;
                logNum++;
            }
            i+=logNum;
            tol = 1;
        }
        return chars.size();
    }
};
2017/12/4 posted in  leetcode

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