[Leetcode] 93, 39, 40
2017-08-30 12:20
344 查看
93. Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:
Given
"25525511135",
return
["255.255.11.135", "255.255.111.35"]. (Order does not matter)
Solution: 深搜+剪枝,到叶子时判断是否为合法IP,中间节点如果非法直接剪枝。合法IP:四节,每节大小在[0,255]范围中。
Code:
class Solution { public: vector<string> restoreIpAddresses(string s) { vector<string> ans; string path = ""; dfs(0, 1, s, path, ans); return ans; } private: void dfs(int index, int step, string& s, string& path, vector<string>& ans){ if(step==4){ if(checkValid(s, index, s.size()-index)){ path += s.substr(index, s.size()-index); ans.push_back(path); } return; } for(int size=1; size<=3 && index+size<=s.size(); size++){ if(checkValid(s, index, size)){ string tmp = path; path += s.substr(index, size); path.push_back('.'); dfs(index+size, step+1, s, path, ans); path = tmp; }else{ break; } } } bool checkValid(string& s, int first, int size){ if(size>3 || size<=0) return false; if(size>1 && s[first]=='0') return false; //注意001这种情况 int num = 0; for(int i=0; i<size; i++){ num = num*10 + s[first]-'0'; first++; } if(num>=0 && num<=255) return true; else return false; } };
39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number(T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set
[2, 3, 6, 7]and
target
7,
A solution set is:
[ [7], [2, 2, 3] ]
Solution: 深搜。
Code:
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> path;
combinationSum(candidates.begin(), candidates.end(), path, ans, target);
return ans;
}
private:
//static bool cmp (int i,int j) { return (i>j); }
int step = 0;
void combinationSum(vector<int>::iterator first, vector<int>::iterator last,
vector<int>& path, vector<vector<int>>& ans, int target){
step ++;
if(target == 0){
ans.push_back(path);
step--;
return;
}
for(auto i=first; i!=last && *i<=target; i++){
vector<int> tmp = path;
for(int t=1; (*i)*t<=target; t++){
path.push_back(*i);
combinationSum(i+1, last, path, ans, target-(*i)*t);
}
path = tmp;
}
step--;
}
};
40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinationsin C where the candidate numbers sums to T.
Each number in C 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.
For example, given candidate set
[10, 1, 2, 7, 6, 1, 5]and target
8,
A solution set is:
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]
Solution: 深搜,将上面的代码稍微修改一下就可以了,注意相等的数不要重复计算可能性。
Code:
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> path;
combinationSum(candidates.begin(), candidates.end(), path, ans, target);
return ans;
}
private:
void combinationSum(vector<int>::iterator first, vector<int>::iterator last,
vector<int>& path, vector<vector<int>>& ans, int target){
if(target == 0){
ans.push_back(path);
return;
}
for(auto i=first; i!=last && *i<=target; i=upper_bound(i, last, *i)){
path.push_back(*i);
combinationSum(i+1, last, path, ans, target-*i);
path.pop_back();
}
}
};
相关文章推荐
- leetcode之深搜递归回溯类之排列与组合类-----77/39/40/216/317 组合 78/90/368 子排列 22/79/93/131 典型递归回溯 46/47 全排列
- <LeetCode OJ> 39 / 40 Combination Sum(I / II)
- LeetCode 39 && 40 Combination Sum I && II 关键在于剪枝,剪枝讨论在末尾
- [LeetCode]Array主题系列{35,39,40,48题}
- leetcode-39/40 Combination Sum / II
- LeetCode39/40/22/77/17/78/51/46/47/79 10道 Backtracking**
- LeetCode 39 && 40 Combination Sum I && II 关键在于剪枝,剪枝讨论在末尾
- LeetCode-40-Combination Sum II ,同39
- leetcode 39|40|216|377. Combination Sum 1|2|3|4
- [*leetcode 39] Combination Sum && [*leetcode 40] Combination Sum II
- leetcode【39+40+216+377 Combination Sum 相关】【python】
- [LeetCode39]Combination Sum 和[LeetCode40]Combination Sum II
- Leetcode 39 40 216 Combination Sum I II III
- LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
- LeetCode 39~40
- Leetcode 39 Combination Sum & 40 Combination Sum II & 216 Combination Sum III & 377 Combination V
- LeetCode 39,40,46,47,78,90 回溯法专题
- LeetCode (13) Pascal's Triangle (杨辉三角 )
- LeetCode93——Restore IP Addresses
- LeetCode之Pascal's Triangle