组合总和——力扣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;
	}
};
相关推荐
Zevalin爱灰灰5 小时前
现代密码学 第二章——流密码【下】
算法·密码学
飞Link7 小时前
大模型长文本的“救命稻草”:深度解析 TurboQuant 与 KV Cache 压缩技术
算法
郝学胜-神的一滴7 小时前
深度学习优化核心:梯度下降与网络训练全解析
数据结构·人工智能·python·深度学习·算法·机器学习
Je1lyfish8 小时前
CMU15-445 (2025 Fall/2026 Spring) Project#3 - QueryExecution
linux·c语言·开发语言·数据结构·数据库·c++·算法
许彰午8 小时前
03-二叉树——从递归遍历到非递归实现
java·算法
Brilliantwxx8 小时前
【C++】 vector(代码实现+坑点讲解)
开发语言·c++·笔记·算法
叼烟扛炮9 小时前
C++第三讲:类和对象(中)
开发语言·c++·类和对象
KuaCpp9 小时前
C++新特性学习
c++·学习
墨染千千秋10 小时前
C/C++ Keywords
c语言·c++
ximu_polaris10 小时前
设计模式(C++)-行为型模式-中介者模式
c++·设计模式·中介者模式