93.复原IP地址
vector<string> all;
string one;
bool isusefulip(string s,int start,int end){
if(start>end)return false;
if(s[start]=='0'&&start!=end)return false;
int k=0;
int tmp=1;
while(end>=start){
int num = s[end] - '0'; // 将字符转换为数字
k += num * tmp;
tmp*=10;
end--;
if(k>255)return false;
}
if(k>=0&&k<=255)return true;
return false;
}
//和字符串一样
void backing(string s,int startindex,int pointnum){
if(pointnum==3){//没想到用点
if(isusefulip(s,startindex,s.size()-1)) all.push_back(s);
return;
}
for(int i=startindex;i<s.size();i++){
if(isusefulip(s,startindex,i)){
s.insert(s.begin()+i+1,'.');
pointnum++;
backing(s,i+2,pointnum);
s.erase(s.begin()+i+1);
pointnum--;
}else break;
}
}
vector<string> restoreIpAddresses(string s) {
backing(s,0,0);
return all;
}
78.子集
vector<vector<int>> all;
vector<int> one;
void backing(vector<int>& nums,int startindex){
all.push_back(one);
if(startindex>=nums.size())return;
for(int i=startindex;i<nums.size();i++){
one.push_back(nums[i]);
backing(nums,i+1);
one.pop_back();
}
}
vector<vector<int>> subsets(vector<int>& nums) {
backing(nums,0);return all;
}
90.子集II
vector<vector<int>> all;
vector<int> one;
void backing(vector<int>& nums, int startindex, vector<bool>& b1) {
all.push_back(one);
if (startindex >= nums.size())
return;
for (int i = startindex; i < nums.size(); i++) {
if (i > 0 && nums[i] == nums[i - 1] && b1[i - 1] == 0) {
continue;
} else {
one.push_back(nums[i]);
b1[i]=true;
backing(nums, i + 1, b1);
one.pop_back();
b1[i]=false;
};
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<bool> b1(nums.size(), false);
sort(nums.begin(), nums.end());
backing(nums, 0, b1);
return all;
}