题目:
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)
Backtracing,注意值必须有效,如“0”有效,而“01”,“001”, “010”等无效,值必须[0, 255]。
C++版:
class Solution { public: vector<string> restoreIpAddresses(string s) { vector<string> results; if(s.length() < 4 || s.length() > 12) return results; int start = max(1, (int)s.length() - 9) - 1; int end = min(3, (int)s.length() - 3) - 1; for(int i = start; i <= end; i++) { string result = s.substr(0, i + 1); if(result.length() == 3 && atoi(result.c_str()) > 255) break; if(result[0] == '0' && result.length() > 1) continue; restoreIP(s.substr(i + 1, s.length() - i - 1), results, result, 2); } return results; } void restoreIP(string s, vector<string>& results, string result, int count) { if(count == 0) { if(s.length() == 3 && atoi(s.c_str()) > 255) return; if(s[0] == '0' && s.length() > 1) return; result += "."; result += s; results.push_back(result); return; } int start = max(1, (int)s.length() - count * 3) - 1; int end = min(3, (int)s.length() - count) - 1; for(int i = start; i <= end; i++) { string temp = result; string part = s.substr(0, i + 1); if(part.length() == 3 && atoi(part.c_str()) > 255) break; if(part[0] == '0' && part.length() > 1) continue; temp += "."; temp += part; restoreIP(s.substr(i + 1, s.length() - i - 1), results, temp, count - 1); } } };
public class Solution { public List<String> restoreIpAddresses(String s) { List<String> results = new ArrayList<String>(); if(s.length() < 4 || s.length() > 12) return results; int start = Math.max(1, s.length() - 9) - 1; int end = Math.min(3, s.length() - 3) - 1; for(int i = start; i <= end; i++) { String result = s.substring(0, i + 1); if(result.length() == 3 && Integer.parseInt(result) > 255) break; if(result.charAt(0) == '0' && result.length() > 1) continue; StringBuffer sb = new StringBuffer(result); restoreIP(s.substring(i + 1, s.length()), results, sb, 2); } return results; } public void restoreIP(String s, List<String> results, StringBuffer sb, int count) { if(count == 0) { if(s.length() == 3 && Integer.parseInt(s) > 255) return; if(s.charAt(0) == '0' && s.length() > 1) return; sb.append("."); sb.append(s); results.add(sb.toString()); return; } int start = Math.max(1, s.length() - count * 3) - 1; int end = Math.min(3, s.length() - count) - 1; for(int i = start; i <= end; i++) { String part = s.substring(0, i + 1); if(part.length() == 3 && Integer.parseInt(part) > 255) break; if(part.charAt(0) == '0' && part.length() > 1) continue; StringBuffer sb1 = new StringBuffer(sb.toString()); sb1.append("."); sb1.append(part); restoreIP(s.substring(i + 1, s.length()), results, sb1, count - 1); } } }
import copy class Solution: # @param {string} s # @return {string[]} def restoreIpAddresses(self, s): if len(s) < 4 or len(s) > 12: return [] start, end, results = max(1, len(s) - 9) - 1, min(3, len(s) - 3), [] for i in range(start, end): result = s[0:i+1] if len(result) == 3 and int(result) > 255: break if result[0] == '0' and len(result) > 1: continue self.restoreIP(s[i+1:], results, result, 2) return results def restoreIP(self, s, results, result, count): if count == 0: if len(s) == 3 and int(s) > 255: return if s[0] == '0' and len(s) > 1: return result += "." result += s results.append(result) start, end = max(1, len(s) - count * 3) - 1, min(3, len(s) - count) for i in range(start, end): temp = copy.copy(result) part = s[0:i+1] if len(part) == 3 and int(part) > 255: break if part[0] == '0' and len(part) > 1: continue temp += "." temp += part self.restoreIP(s[i+1:], results, temp, count - 1)