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. BZOJ_3289_Mato的文件管理_莫队+树状数组

    BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...

  2. Treap与fhq_Treap学习笔记

    1.普通Treap 通过左右旋来维护堆的性质 左右旋是不改变中序遍历的 #include<algorithm> #include<iostream> #include<c ...

  3. FPGA学习笔记(七)——FSM(Finite State Machine,有限状态机)设计

    FPGA设计中,最重要的设计思想就是状态机的设计思想!状态机的本质就是对具有逻辑顺序和时序规律的事件的一种描述方法,它有三个要素:状态.输入.输出:状态也叫做状态变量(比如可以用电机的不同转速作为状态 ...

  4. 全面理解 javascript 的 argements caller callee call apply 之caller

    /** * 演示arguments的用法,如何获取实参数和形数数 */ function argTest(a,b,c,d){ var numargs = arguments.length; // 获取 ...

  5. 分享波面经【2年经验】【linux c++】

    快三个月没写博客了,一直在忙着准备面试和去面试的路上,所以没时间写,也没什么想写的.现在告一段落,就总结一波! 面经 很感谢一些公司能给我面试机会,有的公司真的会拿学历卡人,也不想多说! 17年毕业, ...

  6. 什么是ZooKeeper?

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 上次写了一篇 什么是消息队列?以后,本来想入门一下K ...

  7. 软件开发架构介绍||OSI七层协议之物理层、数据链路层、网络层、传输层(mac地址、ip协议、断开协议、tcp协议之三次握手四次挥手)

    一.网络编程 软件开发架构 C/S架构 C:客户端 想体验服务的时候才会去找服务端体验服务 S:服务端   24小时不间断的提供服务,即时监听,随时待命 B/S架构 B:浏览器    想体验服务的时候 ...

  8. SQL过滤字符后手工注入漏洞测试(第1题)

    https://www.mozhe.cn/bug/detail/a1diUUZsa3ByMkgrZnpjcWZOYVEyUT09bW96aGUmozhe 分析题目,属于时间盲注,这种情况,通常使用sq ...

  9. 【Chrome插件】去掉因使用jsonView插件的弹出窗口"请停用以开发者模式运行的扩展程序"

    前言 小编最近使用jsonView插件时,每次打开谷歌浏览器都会弹出下面的窗口,上网搜索,找到一个非常有效的方法. 解决方法 一.新建一个文本文档 二.复制代码 <# : @echo off c ...

  10. vmware P2V迁移域内windows服务器脱域问题

    windows 域服务器采用vmware converter进行物理机转换为虚拟机,需要在完成后进行退域重新加域操作. 否则,有可能会出现服务器在30天后脱域情况.出现脱域情况,可以通过退域加域的方式 ...