那些年我们一起写过的代码:递归回溯剪枝

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];
    }
};

3.213. 打家劫舍 II - 力扣(LeetCode)

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);
    }
};

5.206. 反转链表 - 力扣(LeetCode)

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;
    }
};

6.用递归写两两交换链表中的节点-CSDN博客

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;
    }
};

11.98. 验证二叉搜索树 - 力扣(LeetCode)

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;

    }
};

14.46. 全排列 - 力扣(LeetCode)

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;
    }
};

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值