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

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