1.1641. 统计字典序元音字符串的数目 - 力扣(LeetCode)
class Solution {
public:
int countVowelStrings(int n) {
int count=0;
vector<int>dp(5,1);
for(int i=1;i<n;i++)
{
for(int j=1;j<5;j++)
{
dp[j]+=dp[j-1];
}
}
for(int i=0;i<5;i++)
{
count+=dp[i];
}
return count;
}
};
2.LCR 089. 打家劫舍 - 力扣(LeetCode)
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size()==0)
{
return 0;
}
if(nums.size()==1)
{
return nums[0];
}
vector<int>dp(nums.size(),0);
dp[0]=nums[0];
dp[1]=max(dp[0],nums[1]);
for(int x=2;x<nums.size();x++)
{
dp[x]=max(dp[x-1],dp[x-2]+nums[x]);
}
return dp[nums.size()-1];
}
};
class Solution {
public:
int rob1(vector<int>&nums,int left,int right)
{
if(left>right)
{
return 0;
}
vector<int>f(nums.size());
auto g=f;
f[left]=nums[left];
for(int i=left+1;i<=right;i++)
{
f[i]=g[i-1]+nums[i];
g[i]=max(f[i-1],g[i-1]);
}
return max(f[right],g[right]);
}
int rob(vector<int>& nums) {
return max(nums[0]+rob1(nums,2,nums.size()-2),rob1(nums,1,nums.size()-1));
}
};
4.面试题 08.06. 汉诺塔问题 - 力扣(LeetCode)
class Solution {
public:
void move(int n,vector<int>& A, vector<int>& B, vector<int>& C)
{
if(n==0)
{
return;
}
move(n-1,A,C,B);
C.push_back(A.back());
A.pop_back();
move(n-1,B,A,C);
}
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
move(A.size(),A,B,C);
}
};
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==nullptr||head->next==nullptr)
{
return head;
}
ListNode*point=reverseList(head->next);;
head->next->next=head;
head->next=nullptr;
return point;
}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head==nullptr||head->next==nullptr)
{
return head;
}
ListNode*newhead=swapPairs(head->next->next);
ListNode*ret=head->next;
head->next->next=head;
head->next=newhead;
return ret;
}
};
7.50. Pow(x, n) - 力扣(LeetCode)
class Solution {
public:
double pow(double x,long n)
{
if(n==0)
{
return 1.0;
}
double tmp=pow(x,n/2);
if(n%2==0)
{
return tmp*tmp;
}
else{
return x*tmp*tmp;
}
}
double myPow(double x, long n) {
if(n>0)
{
return pow(x,n);
}
else
{
return 1.0/pow(x,n);
}
}
};
8.2331. 计算布尔二叉树的值 - 力扣(LeetCode)
class Solution {
public:
bool evaluateTree(TreeNode* root) {
if(root->left==nullptr&&root->right==nullptr)
{
return root->val==0?false:true;
}
bool left=evaluateTree(root->left);
bool right=evaluateTree(root->right);
if(root->val==2)
{
return left||right;
}
if(root->val==3)
{
return left&&right;
}
return root->val;
}
};
9.129. 求根节点到叶节点数字之和 - 力扣(LeetCode)
class Solution {
public:
int dfs(TreeNode*root,int sum)
{
sum=sum*10+root->val;
if(root->left==nullptr&&root->right==nullptr)
{
return sum;
}
int ret=0;
if(root->left)
{
ret+=dfs(root->left,sum);
}
if(root->right)
{
ret+=dfs(root->right,sum);
}
return ret;
}
int sumNumbers(TreeNode* root) {
return dfs(root,0);
}
};
10.LCR 047. 二叉树剪枝 - 力扣(LeetCode)
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(root==nullptr)
{
return nullptr;
}
root->left=pruneTree(root->left);
root->right=pruneTree(root->right);
if(root->left==nullptr&&root->right==nullptr&&root->val==0)
{
root=nullptr;
return root;
}
return root;
}
};
class Solution {
long prev=LONG_MIN;
public:
bool isValidBST(TreeNode* root) {
if(root==nullptr)
{
return true;
}
bool left=isValidBST(root->left);
bool cur=false;
if(root->val>prev)
{
cur=true;
}
prev=root->val;
bool right=isValidBST(root->right);
return cur&&left&&right;
}
};
12.230. 二叉搜索树中第K小的元素 - 力扣(LeetCode)
class Solution {
int count;
int ret;
public:
void dfs(TreeNode*root)
{
if(root==nullptr)
{
return ;
}
dfs(root->left);
count--;
if(count==0)
{
ret=root->val;
}
dfs(root->right);
}
int kthSmallest(TreeNode* root, int k) {
count=k;
dfs(root);
return ret;
}
};
13.257. 二叉树的所有路径 - 力扣(LeetCode)
class Solution {
public:
vector<string>ret;
void dfs(TreeNode*root,string path)
{
path+=to_string(root->val);
if(root->left==nullptr&&root->right==nullptr)
{
ret.push_back(path);
return ;
}
path+="->";
if(root->left)
{
dfs(root->left,path);
}
if(root->right)
{
dfs(root->right,path);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
string path;
dfs(root,path);
return ret;
}
};
class Solution {
public:
vector<int>path;
vector<vector<int>>ret;
vector<int>check;
void dfs(vector<int>&nums)
{
if(path.size()==nums.size())
{
ret.push_back(path);
return ;
}
for(int i=0;i<nums.size();i++)
{
if(check[i]==0)
{
path.push_back(nums[i]);
check[i]=1;
dfs(nums);
path.pop_back();
check[i]=0;
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
check.resize(nums.size(),0);
dfs(nums);
return ret;
}
};