Assign Cookies
"""简单
"""
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int i = 0;
for(int j=0;i<g.size()&&j<s.size();j++){
if(s[j]>=g[i])
i ++;
}
return i;
}
};
Repeated Substring Pattern
class Solution {
public:
bool repeatedSubstringPattern(string s) {
for(int i=1;i<= s.size()/2;i++){
if(s.size()%i==0){
bool temp = true;
for(int j=i;j<s.size();j++)
if (s[j] != s[j%i])
temp = false;
if(temp)
return true;
}
}
return false;
}
};
"""更快的写法
http://www.cnblogs.com/grandyang/p/6087347.html
"""
class Solution {
public:
bool repeatedSubstringPattern(string str) {
int n = str.size();
for (int i = n / 2; i >= 1; --i) {
if (n % i == 0) {
int c = n / i;
string t = "";
for (int j = 0; j < c; ++j) {
t += str.substr(0, i);
}
if (t == str) return true;
}
}
return false;
}
};
"""另一种方法,比较复杂,略
"""
Hamming Distance
"""简单,但是方法有层次性
"""
class Solution {
public:
int hammingDistance(int x, int y) {
int res = 0;
int k = x^y;
while(k){
res ++;
k &= (k-1);
}
return res;
}
};
Island Perimeter
"""简单,类似边缘滤波器--
"""
class Solution {
public:
int help(vector<vector<int>>& grid, int i, int j){
if(i<0||j<0||i>=grid.size()||j>=grid[0].size())
return 0;
else
return grid[i][j];
}
int islandPerimeter(vector<vector<int>>& grid) {
int rows = grid.size();
int cols = grid[0].size();
int res = 0;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(grid[i][j]==1){
if(help(grid,i-1,j)==0)
res++;
if(help(grid,i+1,j)==0)
res++;
if(help(grid,i,j-1)==0)
res++;
if(help(grid,i,j+1)==0)
res++;
}
}
}
return res;
}
};
Heaters
class Solution {
public:
int findRadius(vector<int>& houses, vector<int>& heaters) {
sort(heaters.begin(), heaters.end());
int res = 0;
for(int house:houses){
int left=0, right=heaters.size();
while(left<right){
int mid = left + (right-left)/2;
if(heaters[mid]<house)
left = mid + 1;
else
right = mid;
}
int r1 = (right==heaters.size())?INT_MAX:heaters[right]-house;
int r2 = (right==0)?INT_MAX:house-heaters[right-1];
res = max(res,min(r1,r2));
}
return res;
}
};
Number Complement
class Solution {
public:
int findComplement(int num) {
int res = 0;
int i = 1;
while(num){
res += i*(((num%2)+1)%2);
num /=2;
i *= 2;
}
return res;
}
};
Largest Palindrome Product
"""难
"""
class Solution {
public:
int largestPalindrome(int n) {
int upper = pow(10,n)-1;
int lower = upper/10;
for(int i=upper;i>lower;i--){
string str = to_string(i);
long t = stol(str+string(str.rbegin(),str.rend()));
for(long j=upper;j*j>t;j--){
if(t%j==0)
return t%1337;
}
}
return 9;
}
};
License Key Formatting
"""
简单,但是注意不要从左边增长string,很慢
"""
class Solution {
public:
string licenseKeyFormatting(string S, int K) {
string res = "";
int num = 0;
for(int i=S.size()-1;i>=0;i--){
if(S[i]!='-'){
num ++;
if(S[i]>='a' && S[i]<='z')
S[i] = S[i]-'a'+'A';
res += S[i];
if(num==K && i>0){
num = 0;
res += "-";
}
}
}
if (!res.empty() && res.back() == '-') res.pop_back();
return string(res.rbegin(), res.rend());
}
};
Max Consecutive Ones
"""
"""
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int sum = 0;
int m = 0;
for(int i=0;i<nums.size();i++){
int n=nums[i];
sum+=n;
if(n==0||i==nums.size()-1){
m = max(m,sum);
sum=0;
}
}
return m;
}
};
Construct the Rectangle
"""
"""
class Solution {
public:
vector<int> constructRectangle(int area) {
vector<int> res;
int a = floor(sqrt(area));
for(int i=a;i>=1;i--)
if(area%i==0){
res.push_back(area/i);
res.push_back(i);
break;
}
return res;
}
};
Next Greater Element I
"""仔细体会不同的解法,尤其是用stack的
"""
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
unordered_map<int,int> map,temp;
for(int n:findNums){
map.insert({n,-1});
temp.insert({n,-1});
}
for(int i=0;i<nums.size();i++){
if(temp.find(nums[i])!=temp.end()){
temp[nums[i]] = 0;
}
if(temp.size()==0)
break;
for(auto t:temp){
if(t.second == 0 && nums[i]>t.first){
map[t.first] = nums[i];
temp.erase(t.first);
}
}
}
vector<int> res;
for(int n:findNums){
res.push_back(map[n]);
}
return res;
}
};
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
unordered_map<int,int> map;
stack<int> st;
vector<int> res;
for(int n:nums){
while(!st.empty() && st.top()<n){
map[st.top()] = n;
st.pop();
}
st.push(n);
}
for(int n: findNums){
res.push_back(map[n]!=0?map[n]:-1);
}
return res;
}
};
Keyboard Row
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> res;
unordered_set<char> row1{'q','w','e','r','t','y','u','i','o','p'};
unordered_set<char> row2{'a','s','d','f','g','h','j','k','l'};
unordered_set<char> row3{'z','x','c','v','b','n','m'};
for (string word: words){
int one=0, two=0, three=0;
for(char c: word){
if(c<'a') c+=32;
if(row1.count(c)==1) one=1;
if(row2.count(c)==1) two=1;
if(row3.count(c)==1) three=1;
if(one+two+three>1) break;
}
if(one+two+three==1) res.push_back(word);
}
return res;
}
};