Hamming Distance

利用按位异或操作^来标记不同的位置。
c++
class Solution {
public:
int hammingDistance(int x, int y) {
int tmp = x^y;
int res = 0;
while (tmp) {
if ((tmp>>1)<<1!=tmp) {
res++;
}
tmp>>=1;
}
return res;
}
};

2017/12/7 posted in  leetcode

Keyboard Row

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        set<char> row1 = {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'};
        set<char> row2 = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
        set<char> row3 = { 'z', 'x', 'c', 'v', 'b' ,'n', 'm'};
        vector<set<char>> rows = {row1,row2,row3};
        vector<string> res;
        for (auto str: words) {
            int log = 0;
            for (int i = 0; i<rows.size(); i++) {
                if (rows[i].count((char)tolower(str[0]))>0) {
                    log = i;
                }
            }
            for (int j = 0; j<str.length(); j++) {
                if (rows[log].count((char)tolower(str[j]))==0) {
                    break;
                }
                if (j==str.length()-1) {
                    res.push_back(str);
                }
            }
        }
        return res;
    }
};
2017/12/6 posted in  leetcode

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