Student Attendance Record I

优化前:
c++
bool checkRecord(string s) {
int logA = 0;
for (int i=0; i<s.length(); i++) {
if (s[i]=='A') {
logA++;
if (logA>1) {
return 0;
}
}
if (i>1&&s[i]=='L'&&s[i-1]=='L'&&s[i-2]=='L') {
return 0;
}
}
return 1;
}

优化后:

class Solution {
public:
    bool checkRecord(string s) {
        int count_A = 0;
        int pos = 0;
        while(pos<s.length())
        {
            if(s[pos]=='A')
            {
                count_A++;
                pos++;
                if(count_A>1)
                    return false;
            }
            else if(s[pos]=='L')
            {
                int count = 0;
                while(pos<s.length()&&s[pos]=='L')
                {
                    count++;
                    pos++;
                }
                if(count>2)
                    return false;
            }
            else
                pos++;
            
        }
        return true;
    }
};
2017/12/5 posted in  leetcode

Baseball Game


class Solution {
public:
    int calPoints(vector<string>& ops) {
        int sum = 0, score = 0;
        vector<int> rounds;
        for (string op : ops) {
            if (op == "C") {
                sum -= rounds.back();
                rounds.pop_back();
                continue;
            }
            if (op == "D") {
                sum += score = rounds.back() * 2;
            }
            else if (op == "+") {
                sum += score = rounds[rounds.size() - 1] + rounds[rounds.size() - 2];
            }
            else {
                sum += score = stoi(op);
            }
            rounds.push_back(score);
        }
        return sum;
    }
};
2017/12/5 posted in  leetcode

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