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 combinations in 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();
}
}
};