39

40

78. Subsets https://leetcode.com/problems/subsets/description/

    void subsets(vector<int>& nums, int pos, vector<int>& current, vector<vector<int>>& result){
if(pos == nums.size()){
result.push_back(current);
return;
}else{
subsets(nums,pos+,current,result);
current.push_back(nums[pos]);
subsets(nums,pos+,current,result);
current.pop_back();
}
} vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> result;
vector<int> current;
subsets(nums, , current, result);
return result;
}

使用迭代的方法来做呢?

90. Subsets II https://leetcode.com/problems/subsets-ii/description/

void getsubsetsWithDup(vector<int>& nums, int pos, vector<int>& temp, vector<vector<int>>& result) {
if (pos == nums.size()) {
result.emplace_back(temp);
return;
}
//nextpos指向下一个不为nums[pos]的位置或为nums.size()
int nextpos = pos + ;
while (nextpos != nums.size() && nums[nextpos] == nums[pos])
nextpos++;
getsubsetsWithDup(nums, nextpos, temp, result);
for (int i = pos; i < nextpos; i++) {
temp.emplace_back(nums[i]);
getsubsetsWithDup(nums, nextpos, temp, result);
}
temp.erase(temp.end() - (nextpos - pos), temp.end());
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> result;
vector<int> temp;
getsubsetsWithDup(nums, , temp, result);
return result;
}

1、重复的地方在与多个重复元素在一起的时候会出现前一个在、后一个不再和前一个不在、后一个在的这种重复情况,想要去除就在遇到这种情况的时候直接跳过,用不重复的情况代替,不重复的情况是确定个数的重复元素

216. Combination Sum III https://leetcode.com/problems/combination-sum-iii/description/

class Solution {
public:
/*
pos:遍历到1到9中的哪个位置
current:当前的数组
*/
void combinationSum3(vector<vector<int>>& result, int k ,int n, int pos,vector<int>& current){
if(n == && k== ){
result.push_back(current);
return;
}else if(n <= || k <= || pos > )
return;
else{
for(int i=pos;i<=;i++){
if(n-i<) return;
current.push_back(i);
combinationSum3(result,k-,n-i,i+,current);
current.pop_back();
}
}
}
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> result;
vector<int> current;
combinationSum3(result, k, n, , current);
return result;
}
};

LeetCode递归解题模板的更多相关文章

  1. LeetCode数组解题模板

    一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...

  2. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

  3. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  4. LeetCode: solveSudoku 解题报告

    Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are in ...

  5. leetcode网解题心得——61. 旋转链表

    目录 leetcode网解题心得--61. 旋转链表 1.题目描述 2.算法分析: 3.用自然语言描述该算法 4.java语言实现 5.C语言实现 leetcode网解题心得--61. 旋转链表 1. ...

  6. LeetCode刷题模板(1):《我要打10个》之二分法

    Author       :  叨陪鲤 Email         : vip_13031075266@163.com Date          : 2021.01.23 Copyright : 未 ...

  7. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  8. Recursive - leetcode [递归]

    经验tips: Recursion is the best friend of tree-related problems. 一是只要遇到字符串的子序列或配准问题首先考虑动态规划DP,二是只要遇到需要 ...

  9. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

随机推荐

  1. 用PCA(主成分分析法)进行信号滤波

    用PCA(主成分分析法)进行信号滤波 此文章从我之前的C博客上导入,代码什么的可以参考matlab官方帮助文档 现在网上大多是通过PCA对数据进行降维,其实PCA还有一个用处就是可以进行信号滤波.网上 ...

  2. 【爆料】-《阿伯泰大学毕业证书》Abertay一模一样原件

    ☞阿伯泰大学毕业证书[微/Q:865121257◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归&a ...

  3. 深入剖析最新IE0day漏洞

    在2018年4月下旬,我们使用沙箱发现了IE0day漏洞;自从在野外发现上一个样本(CVE-2016-0189)已经有两年多了.从许多方面来看,这个特别的漏洞及其后续的开发比较有趣.下一篇文章将分析最 ...

  4. qtchooser

    qtchooser 的配置目录: /usr/lib/x86_64-linux-gnu/qtchooser qtchooser 的真实配置目录: /usr/share/qtchooser qtchoos ...

  5. 你真的了解String吗?(修正版)

    修正前:new出来的对象,会在堆中存放真正的值: 大错特错!!!! 修正后:new出来的对象,堆存放的并不是真正的值,而是常量池中字符串常量的地址. 一.抛砖引玉 ​ 不知道大家在做面试题时是否会遇到 ...

  6. Windows环境下消息中间件RabbitMq的搭建与应用

    前言 消息中间件目前已经在很多大型的项目上得到了运用,我们常见的有 RabbitMq, activitymq,kafka,rocketmq,其中rocketmq是阿里自己在kafka的基础上用java ...

  7. Spring:(二)DI依赖注入方式

    DI 依赖注入 DI(Dependency Injection)依赖注入,说简单一点就将类里面的属性在创建类的过程中给属性赋值,即将对象依赖属性(简单值,集合,对象)通过配置设值给该对象. 属性注入的 ...

  8. 设计模式之桥接模式——Java语言描述

    桥接适用于把抽象化和实现化解耦,使得二者可以独立变化.这种类型的设计模式属于结构性模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦 这种模式设计到一个作为桥接的接口,使得实体类的功能独立 ...

  9. HTML 练习滑动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Linux 用户及权限详解

    Linux 用户及权限详解 用户 , 组 ,权限 安全上下文(secure context): 权限: r,w,x 文件: r : 可读,可以使用类似cat 等命令查看文件内容. w : 可写,可以编 ...