标签:git public 位置 选择 backtrac push tmp tput atoi
问题:
给定一串由数字构成的字符串。
给任意两个数字间添加‘.‘,一共加3次,求能得到的所有有效的IP格式。
Example 1: Input: s = "25525511135" Output: ["255.255.11.135","255.255.111.35"] Example 2: Input: s = "0000" Output: ["0.0.0.0"] Example 3: Input: s = "1111" Output: ["1.1.1.1"] Example 4: Input: s = "010010" Output: ["0.10.0.10","0.100.1.0"] Example 5: Input: s = "101023" Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"] Constraints: 0 <= s.length <= 3000 s consists of digits only.
解法:backtracking(回溯算法)
参数:
处理:
代码参考:
1 class Solution { 2 public: 3 bool isValid(string s) { 4 int ipfield = atoi(s.c_str()); 5 string ip = to_string(ipfield); 6 if(ipfield >= 0 && ipfield <= 255 && ip == s) return true; 7 return false; 8 } 9 void backtrack(vector<string>& res, string path, int pos, string s, int n) { 10 if(n == 3) { 11 string tmp = s.substr(pos); 12 if(isValid(tmp)) { 13 res.push_back(path+tmp); 14 } 15 return; 16 } 17 for(int i = pos+1; i < s.size(); i++) { 18 string tmp = s.substr(pos, i-pos); 19 if(isValid(tmp)) { 20 backtrack(res, path+tmp+".", i, s, n+1); 21 } else return; 22 } 23 } 24 vector<string> restoreIpAddresses(string s) { 25 vector<string> res; 26 string path; 27 backtrack(res, path, 0, s, 0); 28 return res; 29 } 30 };
标签:git public 位置 选择 backtrac push tmp tput atoi
原文地址:https://www.cnblogs.com/habibah-chang/p/14242736.html