605.种花问题
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int i = 0, count = 0;
while(i<flowerbed.size()){
if((flowerbed[i]==0) && (i==flowerbed.size()-1 || flowerbed[i+1]==0) && (i==0 || flowerbed[i-1]==0)){
count++;
flowerbed[i]=1;
}
i++;
}
if(count >= n) return true;
else return false;
}
};
606.根据二叉树创建字符串
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
string tree2str(TreeNode* t) {
if(t==NULL) return "";
string res = to_string(t->val);
if(t->left!=NULL)
res += '(' + tree2str(t->left) + ')';
else if(t->right!=NULL)
res += "()";
if(t->right!=NULL)
res += '(' + tree2str(t->right) + ')';
return res;
}
};
617.合并二叉树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if(t1==NULL && t2==NULL) return NULL;
if(t1==NULL) return t2;
if(t2==NULL) return t1;
t1->val += t2->val;
t1->left = mergeTrees(t1->left,t2->left);
t1->right = mergeTrees(t1->right,t2->right);
return t1;
}
};
628.三个数的最大乘积
class Solution {
public:
int maximumProduct(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n = nums.size();
int a = nums[n-1]*nums[n-2]*nums[n-3];
int b = nums[n-1]*nums[0]*nums[1];
return a>b?a:b;
}
};
633.平方数之和
class Solution {
public:
bool judgeSquareSum(int c) {
if(!c) return true;
for(int a = 0; (double)a*a < (double)c; a++){
int b = sqrt(c-a*a);
if(b*b==(c-a*a))
return true;
}
return false;
}
};
637.二叉树的层平均值
/*
*注意sum用long int 防止溢出
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
TreeNode* p;
long int sum;
int node_sum;
while(!q.empty()){
sum = 0;
node_sum = q.size();
for(int i = 0; i < node_sum; ++i){
p = q.front();
q.pop();
sum += p->val;
if(p->left!=NULL) q.push(p->left);
if(p->right!=NULL) q.push(p->right);
}
res.push_back((double)sum/node_sum);
}
return res;
}
};
643.子数组最大平均值
class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
double res = 0;
int tmp = 0;
for(int i = 0; i < nums.size(); ++i){
if(i<k){
tmp += nums[i];
res = tmp;
continue;
}
tmp = tmp + nums[i] - nums[i-k];
if(tmp > res)
res = tmp;
}
return res/k;
}
};
645.错误的集合
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
vector<int> res;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i = 0; i < n-1; ++i){
if(nums[i]==nums[i+1]){
res.push_back(nums[i]);
break;
}
}
int tmp = accumulate(nums.begin(),nums.end(),0);
res.push_back(n*(n+1)/2+res[0]-tmp);
return res;
}
};
653.两数之和IV
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool findTarget(TreeNode* root, int k) {
vector<int> res;
inorder(root,res);
int left = 0, right = res.size()-1;
while(left < right){
int tmp = res[left]+res[right];
if(tmp > k) right--;
else if(tmp < k) left++;
else return true;
}
return false;
}
void inorder(TreeNode* root, vector<int>& res){
if(!root) return;
inorder(root->left,res);
res.push_back(root->val);
inorder(root->right,res);
}
};
657.机器人能否回到原点
class Solution {
public:
bool judgeCircle(string moves) {
int row = 0, col = 0;
for(auto c : moves){
switch(c){
case 'R':
col++;
break;
case 'L':
col--;
break;
case 'U':
row++;
break;
case 'D':
row--;
break;
default:
break;
}
}
return row==0&&col==0;
}
};
661.图片平滑器
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int m = M.size(), n = M[0].size();
vector<vector<int>> res(m,vector<int>(n));
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j){
int count = 0,sum=0;
for(int a = i - 1; a < i + 2; a++)
for(int b = j - 1; b < j + 2; b++){
if(a>=0&&a<m && b>=0&&b<n){
count++;
sum+=M[a][b];
}
}
res[i][j] = sum/count;
}
return res;
}
};
665.非递减数列
/*
*只有当除了这个最大数之外的其他所有数字为非递减数列时为真
*/
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
if(nums.size()<=2) return true;
int count = 0,last = nums[0],lastlast = INT_MIN;
for(int i = 1; i < nums.size(); ++i){
if(nums[i]<last){
count++;
if(count==2) return false;
if(nums[i] >= lastlast) last = nums[i];
continue;
}
lastlast = last;
last = nums[i];
}
return true;
}
};
669.修剪二叉搜索树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root==NULL) return root;
if((root->val) > R) return trimBST(root->left, L, R);
if((root->val) < L) return trimBST(root->right, L, R);
root->left = trimBST(root->left,L,R);
root->right = trimBST(root->right,L,R);
return root;
}
};
671.二叉树中的第二小节点
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int findSecondMinimumValue(TreeNode* root) {
set<int> res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* node = q.front();
q.pop();
res.insert(node->val);
if(node->left!=NULL) {
q.push(node->left);
q.push(node->right);
}
}
if(res.size()>1) {
auto iter = res.begin();
return *(++iter);
}
else return -1;
}
};
674.最长连续递增数列
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if(nums.empty()) return 0;
if(nums.size()==1) return 1;
int res = 1,tmp = 1;
for(int i = 1; i < nums.size(); ++i){
if(nums[i] > nums[i-1]){
tmp++;
}
else{
if(tmp>res) res = tmp;
tmp = 1;
}
}
if(tmp>res) res = tmp;
return res;
}
};
680.验证回文字符串II
class Solution {
public:
bool validPalindrome(string s) {
int left = 0, right = s.length()-1;
while(left < right){
if(s[left]!=s[right]){
return isPal(s,left,right-1) || isPal(s,left+1,right);
}
left++;
right--;
}
return true;
}
bool isPal(string s, int left, int right){
while(left < right){
if(s[left]!=s[right]) return false;
left++;
right--;
}
return true;
}
};
682.棒球比赛
class Solution {
public:
int calPoints(vector<string>& ops) {
vector<int> temp;
for(auto s : ops){
if(s=="C"){
temp.pop_back();
}else if(s=="D"){
temp.push_back(temp.back()*2);
}else if(s=="+"){
temp.push_back(temp.back()+temp[temp.size()-2]);
}else{
temp.push_back(stoi(s));
}
}
return accumulate(temp.begin(),temp.end(),0);
}
};
686.重复叠加字符串匹配
class Solution {
public:
int repeatedStringMatch(string A, string B) {
int n1 = A.size(), n2 = B.size(), count = 1;
string temp = A;
while(temp.size() < n2){
temp += A;
count++;
}
if(temp.find(B)!=string::npos) return count;
temp+=A;
return temp.find(B)!=string::npos?++count:-1;
}
};
687.最长同值路径
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int longestUnivaluePath(TreeNode* root) {
int max = 0;
dfs(root, max);
return max;
}
int dfs(TreeNode* root, int& max){
if(root==NULL) return 0;
int left = dfs(root->left, max);
int right = dfs(root->right, max);
if(root->left!=NULL && root->val == root->left->val){
left++;
}else left = 0;
if(root->right!=NULL && root->val == root->right->val){
right++;
}else right = 0;
if(left+right>max) max = left+right;
return left>right?left:right;
}
};
690.员工的重要性
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector subordinates;
};
*/
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
int res = 0;
for(int i = 0; i < employees.size(); ++i){
if(employees[i]->id == id){
BFS(employees,i,res);
}
}
return res;
}
void BFS(vector<Employee*>& employees, int k, int& res){
queue<Employee*> q;
q.push(employees[k]);
while(!q.empty()){
Employee* node = q.front();
q.pop();
res +=node->importance;
if(node->subordinates.size()!=0){
for(auto id : node->subordinates){
for(int i = 0; i < employees.size(); ++i){
if(employees[i]->id == id){
q.push(employees[i]);
break;
}
}
}
}
}
}
};
693.交替位二进制数
//& | ^ 的优先级要比 == != 低,还是用string方便些
class Solution {
public:
bool hasAlternatingBits(int n) {
if(n==1) return true;
string res;
while(n){
res.push_back(n&1);
n>>=1;
}
for(int i = 1; i < res.length(); ++i){
if(res[i]==res[i-1]) return false;
}
return true;
}
};
696.计算二进制子串
class Solution {
public:
int countBinarySubstrings(string s) {
int ones=0,zeros=0,res=0;
for(int i = 0; i < s.size(); ++i){
if(i==0){
s[i]=='0'?zeros++:ones++;
}else{
if(s[i]=='1'){
ones = s[i-1]=='1'?ones+1:1;
if(zeros>=ones) res++;
}else if(s[i]=='0'){
zeros = s[i-1]=='0'?zeros+1:1;
if(ones>=zeros) res++;
}
}
}
return res;
}
};
697.数组的度
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
int n = nums.size();
map<int,vector<int>> tmp;
for(int i = 0; i < n; ++i){
tmp[nums[i]].push_back(i);
}
int count = 0;
int res = INT_MAX;
for(auto iter = tmp.begin(); iter!=tmp.end(); ++iter){
int m = iter->second.size();
if(m>count){
count = m;
res = iter->second.back() - iter->second[0] + 1;
}else if(m==count){
res = min(res, iter->second.back() - iter->second[0] + 1);
}
}
return res;
}
};
700.二叉搜索树的搜索
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root==NULL) return NULL;
if(root->val==val) return root;
else if(root->val < val) return searchBST(root->right, val);
else return searchBST(root->left, val);
}
};