组合总和——力扣39

文章目录

题目描述



回溯

cpp 复制代码
class Solution {
public:
    vector<vector<int>> res;
	vector<int> seq; 
	
	void dfs(vector<int>& nums, int pos, int target){
		if(target==0){
			res.emplace_back(seq);
			return;
		}
		if(pos==nums.size()){
			return;
		}
		//直接跳过
		dfs(nums, pos+1, target);
		if(target - nums[pos]>=0){
			seq.push_back(nums[pos]);
			dfs(nums, pos, target-nums[pos]);
			seq.pop_back();
		}
	}
	
	vector<vector<int>> combinationSum(vector<int>& candidates, int target){
		dfs(candidates, 0, target);
		return res;
	}
};
相关推荐
Tiandaren37 分钟前
Selenium 4 教程:自动化 WebDriver 管理与 Cookie 提取 || 用于解决chromedriver版本不匹配问题
selenium·测试工具·算法·自动化
岁忧2 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7892 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
秋说4 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
Maybyy4 小时前
力扣61.旋转链表
算法·leetcode·链表
谭林杰5 小时前
B树和B+树
数据结构·b树
卡卡卡卡罗特6 小时前
每日mysql
数据结构·算法
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
蜉蝣之翼❉7 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc
aramae7 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio