组合总和——力扣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;
	}
};
相关推荐
i嗑盐の小F27 分钟前
【IEEE 独立出版,快速EI检索】第四届人工智能、虚拟现实与可视化国际学术会议(AIVRV 2024)
大数据·人工智能·深度学习·算法·机器学习·vr
FITMT1 小时前
STL之list
c++·list
大白的编程日记.3 小时前
【C++笔记】C++编译器拷贝优化和内存管理
java·开发语言·c++·笔记
CrazyZ1264 小时前
c++primer第九章内存模型和名称空间学习笔记
c++·笔记·学习
¥ 多多¥5 小时前
数据结构:内存的使用
linux·c语言·开发语言·数据结构
小灰灰爱代码5 小时前
C++——将数组a[5]={-1,2,9,-5,7}中小于0的元素置成0。并将其结果输出(要求:用数组名作为函数的参数来实现)
数据结构·c++·算法
冰红茶兑滴水5 小时前
Linux 线程互斥
linux·c++
Mopes__7 小时前
Python | Leetcode Python题解之第421题数组中两个数的最大异或值
python·leetcode·题解
liuyang-neu7 小时前
力扣中等 33.搜索旋转排序数组
java·数据结构·算法·leetcode
ganjiee00078 小时前
力扣(leetcode)每日一题 2414 最长的字母序连续子字符串的长度
java·算法·leetcode