组合总和——力扣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;
	}
};
相关推荐
袋鼠云数栈27 分钟前
集团数字化统战实战:统一数据门户与全业态监管体系构建
大数据·数据结构·人工智能·多模态
小月球~1 小时前
天梯赛 · 并查集
数据结构·算法
仍然.1 小时前
算法题目---模拟
java·javascript·算法
史蒂芬_丁2 小时前
Qt, C++数据类型扩展问题
数据库·c++·qt
6Hzlia2 小时前
【Hot 100 刷题计划】 LeetCode 118. 杨辉三角 | C++ 动态规划题解
c++·leetcode·动态规划
三道渊3 小时前
C语言:文件I/O
c语言·开发语言·数据结构·c++
kali-Myon3 小时前
CTFshow-Pwn142-Off-by-One(堆块重叠)
c语言·数据结构·安全·gdb·pwn·ctf·
潇冉沐晴3 小时前
DP——背包DP
算法·背包dp
是有头发的程序猿4 小时前
用Open Claw接口做1688选品、价格监控、货源对比
开发语言·c++·人工智能
GIOTTO情4 小时前
2026 世界互联网大会亚太峰会|AI 时代媒介投放的技术实战与算法优化
人工智能·算法