Repeated String Match

要考虑"abc" "abc" "abc""c abc a"的情况,所以i<=lenB/lenA+2
c++
class Solution {
public:
int repeatedStringMatch(string A, string B) {
int lenA = A.length(),lenB = B.length();
string tmp = A;
for (int i = 1; i<=lenB/lenA+2; i++ , tmp+=A) {
if (tmp.find(B)!=string::npos) {
return i;
}
}
return -1;
}
};

2017/12/6 posted in  leetcode

Number of Segments in a String

class Solution {
public:
    int countSegments(string s) {
        int res = 0;
        for (int i = 0; i<s.length(); i++) {
            if (s[i]!=' '&& (s[i+1]==' '|| i==s.length()-1)) {
                res++;
            }
        }
        return res;
    }
};
2017/12/6 posted in  leetcode

Battleships in a Board

class Solution {
public:
    int countBattleships(vector<vector<char>>& board) {
        int m = board.size();
        int n = board[0].size();
        int res = 0;
        for (int i=0; i<m; i++) {
            for (int j =0; j<n; j++) {
                if ((board[i][j]=='X')&&(i==0||board[i-1][j]!='X')&&(j==0||board[i][j-1]!='X')) {
                    ++res;
                }
            }
        }
        return res;
    }
};
2017/12/5 posted in  leetcode

Complex Number Multiplication

第一次写的复杂版:

string complexNumberMultiply(string a, string b) {
    int a1=0 ,b1=0 ,a2=0 ,b2=0;
    for (int i = 0; i<a.length(); i++) {
        int log = 0;
        if (a[i]=='+') {
            log = i;
            string tmp;
            for (int j=0; j<i; j++) {
                tmp+=a[j];
            }
            sscanf(tmp.c_str(), "%d",&a1);
            tmp = "";
            for (int m = i+1; m<a.length()-1; m++) {
                tmp+=a[m];
            }
            sscanf(tmp.c_str(), "%d",&b1);
        }
    }
    for (int i = 0; i<b.length(); i++) {
        int log = 0;
        if (b[i]=='+') {
            log = i;
            string tmp;
            for (int j=0; j<i; j++) {
                tmp+=b[j];
            }
            sscanf(tmp.c_str(), "%d",&a2);
            tmp = "";
            for (int m = i+1; m<b.length()-1; m++) {
                tmp+=b[m];
            }
            sscanf(tmp.c_str(), "%d",&b2);
        }
    }
    string res = to_string(a1*a2-b1*b2)+"+"+to_string(a1*b2+a2*b1)+"i";
    return res;
}

精简版:

class Solution {
public:
    string complexNumberMultiply(string a, string b) {
        int c,d,e,f;
        char ret[100];
        sscanf(a.c_str(),"%d+%di",&c,&d);
        sscanf(b.c_str(),"%d+%di",&e,&f);
        cout<<c<<" "<<d<<" "<<e<<" "<<f<<endl;
        sprintf(ret,"%d+%di",(c*e-f*d),(d*e+c*f));
        string ans(ret);
        return ans;
    }
};
2017/12/5 posted in  leetcode

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