backTrack
leetcode中常見的backTrack類題目:combination、subsets、permutation、Palindrome Partitioning.
1、combination
<1>leetcode39. Combination Sum
題目描述:
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates?where the candidate numbers sums to target.
Each number in candidates?may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
void backTrack(vector<vector<int>>& ans, vector<int>& tmp, const vector<int>& nums, int remain, int start){if(remain < 0) return;else if(remain == 0) ans.push_back(tmp);else{for(int i = start; i < nums.size(); i++){tmp.push_back(nums[i]);backTrack(ans, tmp, nums, remain - nums[i], i);tmp.pop_back();}} }vector<vector<int>> combinationSum(vector<int>& nums, int target){vector<vector<int>> ans;vetcot<int> temp;sort(nums.begin(), nums.end());backTrack(ans, temp, nums, target, 0);return ans; }
<2>leetcode40. Combination Sum I
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates?where the candidate numbers sums to target.
Each number in candidates?may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
總結
 
                            
                        - 上一篇: gamit批量下载精密星历shell脚本
- 下一篇: 武汉理工大学-随机过程-2020年期末复
