Next Greater Element I

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
        vector<int> res;
        for(auto num1:findNums){
            for (int i = 0; i<nums.size(); i++) {
                int log = 0;
                if (nums[i]==num1) {
                    for (int j = i; j<nums.size(); j++) {
                        if (nums[j]>num1) {
                            res.push_back(nums[j]);
                            log = 1;
                            break;
                        }
                    }
                    if (log == 0) res.push_back(-1);
                    break;
                }
            }
        }
        return res;
    }
};
2017/12/7 posted in  leetcode

Island Perimeter

class Solution {
public:
    int islandPerimeter(vector<vector<int>>& grid) {
        int squNum = 0 ,log = 0;
        for (int i = 0; i<grid.size(); i++) {
            for (int j = 0; j<grid[0].size(); j++) {
                if (grid[i][j]==1) {
                    squNum++;
                    if (i!=0&&grid[i-1][j]==1) {
                        log+=2;
                    }
                    if (j!=grid[0].size()-1&&grid[i][j+1]==1) {
                        log+=2;
                    }
                }
            }
        }
        return squNum*4-log;
    }
};
2017/12/7 posted in  leetcode

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