组合总和——力扣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;
	}
};
相关推荐
凉茶钱9 分钟前
【吃透C++】万字解析类和对象
开发语言·c++
白杨尚青31 分钟前
C++入门篇(十):string(上)——认识string:构造与三大遍历(一条龙讲透operator[]、迭代器、auto、范围for)
java·开发语言·c++·笔记·stl
FlightYe35 分钟前
视界原理之2D视频(二):视频文件里有什么
android·linux·网络·c++·ffmpeg·音视频·aac
ychqsq41 分钟前
198.根源
经验分享·职场和发展
Chen—LSN41 分钟前
C语言——⽂件操作(1)
c语言·开发语言·c++·经验分享·笔记·算法·链表
anew___1 小时前
《从零手写操作系统 (11):文件系统初探——initrd与VFS抽象层》
java·javascript·算法
辛苦才能1 小时前
C++ map/set 深度解析:从关联式容器的本质到 operator[] 的三重身份
开发语言·c++
Chen—LSN1 小时前
C语言——文件操作(2)
c语言·开发语言·c++·经验分享·笔记·算法·c#
纪念 2291 小时前
C++类和对象(最终篇)
开发语言·c++
程序员yu2 小时前
会编网络:别被入门教程骗了,Python实用能力不在语法本身
开发语言·python·学习·算法