Subtree of Another Tree
class Solution {
public:
bool isSame(TreeNode* s, TreeNode* t){
if(!s && !t)
return true;
if(s && t)
return (s->val == t->val) && isSame(s->left, t->left) && isSame(s->right, t->right);
return false;
}
bool isSubtree(TreeNode* s, TreeNode* t) {
if(!s)
return false;
if(isSame(s,t))
return true;
return isSubtree(s->left, t) || isSubtree(s->right, t);
}
};
Distribute Candies
class Solution {
public:
int distributeCandies(vector<int>& candies) {
unordered_map<int,int> map;
for(int c:candies)
map[c] ++;
return min(candies.size()/2, map.size());
}
};
Shortest Unsorted Continuous Subarray
"""O(n)的解法比较复杂:
http://www.cnblogs.com/grandyang/p/6876457.html
"""
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
vector<int> a = nums;
sort(a.begin(),a.end());
int i=0;
while(nums[i]==a[i])
i++;
int j=nums.size()-1;
while(nums[j]==a[j])
j--;
return max(0, j-i+1);
}
};
Longest Harmonious Subsequence
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int> m;
int res = 0;
for(int n:nums){
m[n]++;
res = m[n-1]==0?res:max(res, m[n]+m[n-1]);
res = m[n+1]==0?res:max(res, m[n]+m[n+1]);
}
return res;
}
};
Range Addition II
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
int min_a = m;
int min_b = n;
for(vector<int> v:ops){
min_a = min(min_a, v[0]);
min_b = min(min_b, v[1]);
}
return min_a*min_b;
}
};
Minimum Index Sum of Two Lists
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
unordered_map<string, int> map;
for(int i=0;i<list1.size();i++)
map[list1[i]] = i;
vector<string> res;
int sum = 3000;
for(int i=0;i<list2.size();i++){
if(map.count(list2[i])){
int temp = i+map[list2[i]];
if(sum==temp)
res.push_back(list2[i]);
if(sum>temp){
res.clear();
res.push_back(list2[i]);
sum=temp;
}
}
}
return res;
}
};
Can Place Flowers
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
for(int i=0; i<flowerbed.size();i++){
if(flowerbed[i]==0){
if(i==0 && flowerbed[i+1]==0){
n--;
flowerbed[i]=1;
}
else if(i==flowerbed.size()-1 && flowerbed[i-1]==0){
n--;
flowerbed[i]=1;
}
else if(flowerbed[i+1]==0 && flowerbed[i-1]==0){
n--;
flowerbed[i]=1;
}
}
}
return n<=0;
}
};
Construct String from Binary Tree
class Solution {
public:
void helper(TreeNode* node, string & res){
if(!node)
return;
res += "(";
res += to_string(node->val);
if(!node->left && node->right)
res += "()";
helper(node->left, res);
helper(node->right, res);
res += ")";
}
string tree2str(TreeNode* t) {
if(!t)
return "";
string res="";
helper(t, res);
return string(res.begin() + 1, res.end() - 1);
}
};
Merge Two Binary Trees
class Solution {
public:
void mergeChild(TreeNode* n1, TreeNode* n2){
if(!n1 || !n2)
return;
if(!n1->left)
n1->left = n2->left;
else{
if(n1->left && n2->left)
n1->left->val += n2->left->val;
mergeChild(n1->left, n2->left);
}
if(!n1->right)
n1->right = n2->right;
else{
if(n1->right && n2->right)
n1->right->val += n2->right->val;
mergeChild(n1->right, n2->right);
}
}
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if(!t1)
return t2;
if(!t2)
return t1;
t1->val += t2->val;
mergeChild(t1, t2);
return t1;
}
};
Maximum Product of Three Numbers
"""叔叔这题我们不排序,居然没有排序快--
"""
class Solution {
public:
int maximumProduct(vector<int>& nums) {
int res;
int max1=INT_MIN, max2=INT_MIN, max3=INT_MIN;
int min1=INT_MAX, min2=INT_MAX;
for(int n:nums){
if(n>max3){
if(n<=max2){
max3 = n;
}
else if(n<=max1){
max3 = max2;
max2 = n;
}
else{
max3 = max2;
max2 = max1;
max1 = n;
}
}
if(n<min2){
if(n>=min1)
min2 = n;
else{
min2 = min1;
min1 = n;
}
}
}
return max(max1*max2*max3,max1*min1*min2);
}
};
Sum of Square Numbers
class Solution {
public:
bool judgeSquareSum(int c) {
int a = 0, b = sqrt(c);
while (a <= b) {
if (a * a + b * b == c) return true;
else if (a * a + b * b < c) ++a;
else --b;
}
return false;
}
};
Average of Levels in Binary Tree
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
if(!root)
return {};
vector<double> res;
queue<TreeNode*> q;
q.push(root);
int n;
double s;
while(!q.empty()){
n = q.size();
s = 0;
for(int i=0;i<n;i++){
TreeNode* t = q.front();
q.pop();
s += t->val;
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
res.push_back(s/n);
}
return res;
}
};
Maximum Average Subarray I
class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
double res = 0, res2;
for(int i=0;i<k;i++)
res += nums[i];
res2 = res;
for(int i=k;i<nums.size();i++){
res = res-nums[i-k]+nums[i];
res2 = max(res2, res);
}
return res2/k;
}
};
Set Mismatch
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
vector<int> res(2,-1);
for(int i:nums){
if(nums[abs(i)-1]<0)
res[0] = abs(i);
else
nums[abs(i)-1] *= -1;
}
for(int i=0;i<nums.size();i++){
if(nums[i]>0)
res[1] = i+1;
}
return res;
}
};
Two Sum IV - Input is a BST
class Solution {
public:
bool findTarget(TreeNode* root, int k) {
if(!root)
return false;
queue<TreeNode*> q;
set<int> s;
q.push(root);
while(!q.empty()){
TreeNode* node = q.front();
q.pop();
if(s.count(k-node->val))
return true;
s.insert(node->val);
if(node->left)
q.push(node->left);
if(node->right)
q.push(node->right);
}
return false;
}
};
Judge Route Circle
class Solution {
public:
bool judgeCircle(string moves) {
int ud = 0;
int lf = 0;
for(char m:moves){
if(m=='U')
ud++;
else if(m=='D')
ud--;
else if(m=='L')
lf--;
else if(m=='R')
lf++;
}
return ud==0&&lf==0;
}
};
Image Smoother
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
vector<vector<int>> res = M;
vector<vector<int>> dirs{{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,0}};
int row = M.size();
int col = M[0].size();
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
int sum=0;
int num=0;
for(auto dir:dirs){
int x = i+dir[0];
int y = j+dir[1];
if(x>=0&&x<row&&y>=0&&y<col){
sum += M[x][y];
num ++;
}
}
res[i][j] = sum/num;
}
}
return res;
}
};
Non-decreasing Array
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
int n=0;
vector<int> nums2 = nums;
for(int i=1;i<nums.size();i++){
if(nums[i]<nums[i-1]){
nums[i] = nums[i-1];
n++;
}
}
int m=0;
for(int i=1;i<nums2.size();i++){
if(nums2[i]<nums2[i-1]){
m++;
if(i==1)
nums2[i-1] = nums2[i];
else{
nums2[i-1] = nums2[i-2];
if(nums2[i]<nums2[i-1])
m++;
}
}
}
return n<=1 || m<=1;
}
};
Trim a Binary Search Tree
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if (!root) return NULL;
if (root->val < L) return trimBST(root->right, L, R);
if (root->val > R) return trimBST(root->left, L, R);
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
};