93.复原ip地址
寻找切割点,但是需要注意的是,切割点(即.号)只有三个
将需要拼凑的值先放进一个数组里,等满足条件后再将其拼成字符串
class Solution {
public:vector<string> res;vector<int> path;int check(string s) {if (s.size() > 1 && s[0] == '0')return -1;if (s.size() == 0)return -1;int sum = 0;for (int i = 0; i < s.size(); i++) {if (s[i] > '9' || s[i] < '0')return -1;sum = sum * 10 + s[i] - '0';if (sum > 255)return -1;}return sum;}void backTracking(string s, int step, int dotNum) {if (dotNum == 0) {string temp = s.substr(step, s.size() - step);int t = check(temp);if (t == -1)return;path.push_back(t);string r = to_string(path[0]);for (int i = 1; i < path.size(); i++) {r = r + "." + to_string(path[i]);}res.push_back(r);path.pop_back();return;}for (int i = step; i < s.size(); i++) {string temp = s.substr(step, i - step + 1);int t = check(temp);if (t == -1)break;path.push_back(t);backTracking(s, i + 1, dotNum - 1);path.pop_back();}}vector<string> restoreIpAddresses(string s) {backTracking(s, 0, 3);return res;}
};
go代码
var (res []stringpath []int
)func check(s string) int {if len(s) > 1 && s[0] == '0' {return -1}if len(s) == 0 {return -1}sum := 0for i := 0; i < len(s); i++ {if s[i] > '9' || s[i] < '0' {return -1}sum = sum*10 + int(s[i]-'0')if sum > 255 {return -1}}return sum
}
func backTracking(s string, step int, dotNum int) {if dotNum == 0 {var temp string = s[step:]t := check(temp)if t == -1 {return}path = append(path, t)r := strconv.Itoa(path[0])for i := 1; i < len(path); i++ {r += "." + strconv.Itoa(path[i])}res = append(res, r)path = path[:len(path)-1]return}for i := step; i < len(s); i++ {temp := s[step : i+1]t := check(temp)if t == -1 {break}path = append(path, t)backTracking(s, i+1, dotNum-1)path = path[:len(path)-1]}
}
func restoreIpAddresses(s string) []string {res = []string{}path = []int{}backTracking(s, 0, 3)return res
}
78.子集
class Solution {
public:vector<int> path;vector<vector<int>> res;void backTracking(vector<int> nums, int step) {res.push_back(path);if (step >= nums.size()) {return;}for (int i = step; i < nums.size(); i++) {path.push_back(nums[i]);backTracking(nums, i + 1);path.pop_back();}}vector<vector<int>> subsets(vector<int>& nums) {backTracking(nums, 0);return res;}
};
go语言
var (res [][]intpath []int
)func backTracking(nums []int, step int) {p := make([]int, len(path))copy(p, path)res = append(res, p)if step >= len(nums) {return}for i := step; i < len(nums); i++ {path = append(path, nums[i])backTracking(nums, i+1)path = path[:len(path)-1]}
}
func subsets(nums []int) [][]int {res = [][]int{}path = []int{}backTracking(nums, 0)return res
}
90.子集II
和之前的题目一样,需要先排序,然后去重
去重可以用used数组,也可以用step来去重
使用used数组:
class Solution {
public:vector<int> path;vector<vector<int>> res;void backTracking(vector<int>& nums, int step,vector<bool>& used) {res.push_back(path);if (step >= nums.size())return;for (int i = step; i < nums.size(); i++) {if(i > 0 && used[i-1] == false && nums[i] == nums[i-1]) continue;used[i] = true;path.push_back(nums[i]);backTracking(nums,i+1,used);used[i] = false;path.pop_back();}}vector<vector<int>> subsetsWithDup(vector<int>& nums) {sort(nums.begin(), nums.end());vector<bool> used(nums.size(),false);backTracking(nums,0,used);return res;}
};
直接使用step去重,只不过判断的逻辑需要变动一下,这样可以保证在同一树层里不使用相同的数
class Solution {
public:vector<int> path;vector<vector<int>> res;void backTracking(vector<int>& nums, int step) {res.push_back(path);if (step >= nums.size())return;for (int i = step; i < nums.size(); i++) {if (i > step && nums[i] == nums[i - 1])continue;path.push_back(nums[i]);backTracking(nums, i + 1);path.pop_back();}}vector<vector<int>> subsetsWithDup(vector<int>& nums) {sort(nums.begin(), nums.end());backTracking(nums, 0);return res;}
};
下面给上go代码:
var (res [][]intpath []int
)func backTracking(nums []int, step int) {p := make([]int, len(path))copy(p, path)res = append(res, p)if step >= len(nums) {return}for i := step; i < len(nums); i++ {if i > step && nums[i] == nums[i-1] {continue}path = append(path, nums[i])backTracking(nums, i+1)path = path[:len(path)-1]}
}
func subsetsWithDup(nums []int) [][]int {res = [][]int{}path = []int{}sort.Ints(nums)backTracking(nums, 0)return res
}