当前位置:网站首页>回溯相关问题
回溯相关问题
2022-06-27 18:05:00 【无敌的神龙战士】
77. 组合
给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
你可以按 任何顺序 返回答案。
示例 1:
输入:n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution {
vector<vector<int>> res;
vector<int> temp;
public:
vector<vector<int>> combine(int n, int k) {
int start = 1;
helper(n, k, start);
return res;
}
void helper(int n, int k, int start){
if(temp.size() == k){
res.push_back(temp);
return;
}
for(int i = start; i <= n; ++i){
temp.push_back(i);
helper(n, k, start + 1);
temp.pop_back();
}
}
};
216. 组合总和 III
找出所有相加之和为 n 的 k 个数的组合,且满足下列条件:
只使用数字1到9
每个数字 最多使用一次
返回 所有可能的有效组合的列表 。该列表不能包含相同的组合两次,组合可以以任何顺序返回。
示例 1:
输入: k = 3, n = 7
输出: [[1,2,4]]
解释:
1 + 2 + 4 = 7
没有其他符合的组合了。
示例 2:
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
解释:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
没有其他符合的组合了。
示例 3:
输入: k = 4, n = 1
输出: []
解释: 不存在有效的组合。
在[1,9]范围内使用4个不同的数字,我们可以得到的最小和是1+2+3+4 = 10,因为10 > 1,没有有效的组合。
class Solution {
vector<vector<int>> res;
vector<int> path;
public:
vector<vector<int>> combinationSum3(int k, int n) {
int start = 1;
help(k, n, start);
return res;
}
void help(int k, int n, int start){
if(path.size() == k){
if(sum(path) == n){
res.push_back(path);
return;
}
}
for(int i = start; i <= 9; ++i){
path.push_back(i);
help(k, n, i + 1);
path.pop_back();
}
}
int sum(vector<int> &p){
int result = 0;
for(int i = 0; i < p.size(); ++i){
result += p[i];
}
return result;
}
};
17. 电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = “23”
输出:[“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]
示例 2:
输入:digits = “”
输出:[]
class Solution {
unordered_map<char, string> map{
{'1', ""}, {'2', "abc"},{'3', "def"},
{'4',"ghi"},{'5', "jkl"}, {'6', "mno"},
{'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"}
};
vector<string> res;
public:
vector<string> letterCombinations(string digits) {
if(digits.size() == 0){
return {};
}
string str = "";
int start = 0;
helper(digits, start, str);
return res;
}
void helper(string &digits, int start, string &str){
if(str.size() == digits.size()){
res.push_back(str);
return;
}
string temp = map[digits[start]];
for(int i = 0; i < temp.size(); ++i){
str.push_back(temp[i]);
helper(digits, start + 1, str);
str.pop_back();
}
}
};
- 组合总和
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
class Solution {
vector<vector<int>> res;
vector<int> path;
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
int start = 0;
helper(candidates, target, start);
return res;
}
void helper(vector<int> &candidates, int target, int start){
if(sum(path) == target){
res.push_back(path);
return;
}
if(sum(path) > target){
return;
}
for(int i = start; i < candidates.size(); ++i){
path.push_back(candidates[i]);
helper(candidates, target, i);
path.pop_back();
}
}
int sum(vector<int> &path){
int sum = 0;
for(auto p : path){
sum += p;
}
return sum;
}
};
40. 组合总和 II
给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
注意:解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
class Solution {
vector<vector<int>> res;
vector<int> path;
vector<bool> visited;
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
int start = 0;
helper(candidates, target, start);
return res;
}
void helper(vector<int> &candidates, int target, int start){
if(sum(path) > target){
return;
}
if(sum(path) == target){
res.push_back(path);
return;
}
for(int i = start; i < candidates.size(); ++i){
if(i > start && candidates[i] == candidates[i - 1]){
continue;
}
// visited[i] = true;
path.push_back(candidates[i]);
helper(candidates, target, i + 1);
// visited[i] = false;
path.pop_back();
}
}
int sum(vector<int> &vec){
int sum = 0;
for(auto v : vec){
sum += v;
}
return sum;
}
};
131. 分割回文串
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
回文串 是正着读和反着读都一样的字符串。
示例 1:
输入:s = “aab”
输出:[[“a”,“a”,“b”],[“aa”,“b”]]
示例 2:
输入:s = “a”
输出:[[“a”]]
class Solution {
vector<vector<string>> res;
vector<string> path;
string temp = "";
public:
vector<vector<string>> partition(string s) {
int start = 0;
helper(s, start);
return res;
}
void helper(string &s, int start){
if(start >= s.size()){
res.push_back(path);
return;
}
for(int i = start; i < s.size(); ++i){
if(judge(s, start, i)){
string temp = s.substr(start, i - start + 1);
path.push_back(temp);
} else {
continue;
}
helper(s, i + 1);
// temp.pop_back();
path.pop_back();
}
}
bool judge(string &s, int start, int end){
for(int i = start, j = end; i < j; ++i, --j){
if(s[i] != s[j]){
return false;
}
}
return true;
}
};
90. 子集 II
给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
示例 1:
输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
示例 2:
输入:nums = [0]
输出:[[],[0]]
class Solution {
vector<vector<int>> res;
vector<int> path;
public:
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
sort(nums.begin(), nums.end());
helper(nums, 0);
return res;
}
void helper(vector<int> &nums, int start){
if(start > nums.size()){
return;
}
res.push_back(path);
for(int i = start; i < nums.size(); ++i){
if(i > start && nums[i] == nums[i - 1]){
continue;
}
path.push_back(nums[i]);
helper(nums, i + 1);
path.pop_back();
}
}
};
491. 递增子序列
给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。
数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。
示例 1:
输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
示例 2:
输入:nums = [4,4,3,2,1]
输出:[[4,4]]
class Solution {
vector<vector<int>> res;
vector<int> path;
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
helper(nums, 0);
return res;
}
void helper(vector<int> &nums, int start){
if(path.size() >= 2){
res.push_back(path);
// return;
}
set<int> set;
for(int i = start; i < nums.size(); ++i){
if(set.find(nums[i]) != set.end()){
continue;
}
if(!path.empty() && nums[i] < path.back()){
continue;
}
set.insert(nums[i]);
path.push_back(nums[i]);
helper(nums, i + 1);
path.pop_back();
}
}
};
47. 全排列 II
给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
示例 1:
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]
示例 2:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
class Solution {
vector<vector<int>> res;
vector<int> path;
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<bool> visited(nums.size(), false);
helper(nums, visited);
return res;
}
void helper(vector<int> &nums, vector<bool> &visited){
if(path.size() == nums.size()){
res.push_back(path);
return;
}
for(int i = 0; i < nums.size(); ++i){
if(i > 0 && nums[i] == nums[i - 1] && visited[i - 1] == false){
continue;
}
if(visited[i] == true){
continue;
}
visited[i] = true;
path.push_back(nums[i]);
helper(nums, visited);
visited[i] = false;
path.pop_back();
}
}
};
边栏推荐
- On thread safety
- Where to look at high-yield bank financial products?
- Running lantern experiment based on stm32f103zet6 library function
- Is it safe to buy stocks and open an account on the account opening link of the securities manager? Ask the great God for help
- Longitude and latitude analysis
- Data intelligence enters the "deep water area", and data governance is the key
- Bit. Store: long bear market, stable stacking products may become the main theme
- 散列表(Hash)-复习
- 运算符的基础知识
- crontab的学习随笔
猜你喜欢
从感知机到前馈神经网络的数学推导
GIS remote sensing R language learning see here
数智化进入“深水区”,数据治理是关键
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
MySQL初学者福利
指针和结构体
Data intelligence enters the "deep water area", and data governance is the key
The Fifth Discipline: the art and practice of learning organization
Summary of submarine cable detection technology
Bit. Store: long bear market, stable stacking products may become the main theme
随机推荐
Common shell script commands (III)
ABAP随笔-面试回忆 望大家 需求不增 人天飙升
shell脚本常用命令(四)
308. 二维区域和检索 - 可变 线段树/哈希
华大单片机KEIL报错_WEAK的解决方案
Leetcode 1381. 设计一个支持增量操作的栈
Pyhton crawls Baidu library text and writes it into word document
Code and principle of RANSAC
作为软件工程师,给年轻时的自己的建议(下)
【debug】平台工程接口调试
1023 Have Fun with Numbers
实战回忆录:从Webshell开始突破边界
A simple calculation method of vanishing point
数仓的字符截取三胞胎:substrb、substr、substring
作用域-Number和String的常用Api(方法)
Leetcode 821. 字符的最短距离(简单) - 续集
Erreur Keil de Huada Single Chip Computer La solution de Weak
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
MySQL表的增删改查(基础)
Leetcode 989. 数组形式的整数加法(简单)