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. NOIP2017 酱油送命记

    Day0 一天,在机房,有点考前的紧张和慌张,打了一下午的模板,立了3个不该立的flag... Day1 拿到试题,万分紧张,T1是数论啊 害怕,一直以为D2T1才是数论,仔细观察却发现(flag1: ...

  2. monkey-----停止正在测试的monkey

    第一步:adb   shell ps | grep monkey:查找到正在测试的monkey包名   第二步:kill   pid:删除查找出的monkey进程   以上完美的停止monkey测试

  3. css3 深入理解flex布局

    一.简要介绍 css3最喜欢的新属性之一便是flex布局属性,用六个字概括便是简单.方便.快速. flex( flexible box:弹性布局盒模型),是2009年w3c提出的一种可以简洁.快速弹性 ...

  4. im2col:将卷积运算转为矩阵相乘

    目录 im2col实现 优缺点分析 参考 博客:blog.shinelee.me | 博客园 | CSDN im2col实现 如何将卷积运算转为矩阵相乘?直接看下面这张图,以下图片来自论文High P ...

  5. SpringBoot之旅第五篇-数据访问

    一.引言 大部分系统都离不开数据访问,数据库包括SQL和NOSQL,SQL是指关系型数据库,常见的有SQL Server,Oracle,MySQL(开源),NOSQL是泛指非关系型数据库,常见的有Mo ...

  6. JS实现页面复制文字时自动加版权

    经亲自实践,尝试了各种方法,目前可行的方法主要有如下两种: 可以在任何运行使用js代码的网站中使用,比如本人在自己的博客园博客中实现了一下,读者您可亲自在本人博客上测试. 方法1: <scrip ...

  7. Java进阶篇设计模式之九----- 解释器模式和迭代器模式

    前言 在上一篇中我们学习了行为型模式的责任链模式(Chain of Responsibility Pattern)和命令模式(Command Pattern).本篇则来学习下行为型模式的两个模式, 解 ...

  8. Redis的正确使用姿势

    前言 说到分布式缓存,可能大多数人脑海浮现的就是redis了,为什么redis能够在竞争激烈的缓存大战中脱颖而出呢?原因无非有一下几点:性能好,丰富的特性跟数据结构,api操作简单.但是用的人多了,就 ...

  9. C# 插入超链接到PDF文档(3种情况)

    超链接可以实现不同元素之间的连接,用户可以通过点击被链接的元素来激活这些链接.具有高效.快捷.准确的特点.本文中,将分享通过C#编程在PDF文档中插入超链接的方法.内容包含以下要点: 插入网页链接 插 ...

  10. 【原】无脑操作:TypeScript环境搭建

    概述:本文描述TypeScript环境搭建,以及基于VSCode的自动编译设置和调试设置.网络上很多相应文章的方式过时了或者无法试验成功. ------------------------------ ...