[Leetcode 74] 92 Restore IP Addresses
2013-07-22 12:53
453 查看
Problem:
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given
return
Analysis:
Originally, I thought DFS may be a good solution. But since the brute-force enumeration is not that complicated, So I tried brute force method. The key observation is that:
1. to restore the IP address, we need to divide the original string into four sub-parts, thus need to choose 3 positions in the string to divide it.
2. for each substring, we need to judge whether it's valid or not. The rule is:
a. the value of the substring should in the range of [0, 255]
b. if it's not a 0, then no leading 0 is allowed. thus "010", "01", "00", "000" are invalid.
3. to get the final IP address, we need only concatinate as follows: s1 + "." + s2 + "." + s3 + "." + s4
For the dividing part, we can make use of some knowledge of IP address to reduce the work:
1. length of IP address should between 4 and 12 inclusively. Any given string's length less than 4 or greater than 12 can not be used as an IP address.
2. each sub-part's length should between 1 and 4 inclusively. Thus each for loop only need to check 3 division positions.
Code:
View Code
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)
Analysis:
Originally, I thought DFS may be a good solution. But since the brute-force enumeration is not that complicated, So I tried brute force method. The key observation is that:
1. to restore the IP address, we need to divide the original string into four sub-parts, thus need to choose 3 positions in the string to divide it.
2. for each substring, we need to judge whether it's valid or not. The rule is:
a. the value of the substring should in the range of [0, 255]
b. if it's not a 0, then no leading 0 is allowed. thus "010", "01", "00", "000" are invalid.
3. to get the final IP address, we need only concatinate as follows: s1 + "." + s2 + "." + s3 + "." + s4
For the dividing part, we can make use of some knowledge of IP address to reduce the work:
1. length of IP address should between 4 and 12 inclusively. Any given string's length less than 4 or greater than 12 can not be used as an IP address.
2. each sub-part's length should between 1 and 4 inclusively. Thus each for loop only need to check 3 division positions.
Code:
class Solution { public: vector<string> restoreIpAddresses(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<string> res; if (s.length() < 4 || s.length() > 12) return res; int len = s.length(); for (int i=1; i<4 && i<len; i++) for (int j=i+1; j<i+4 && j<len; j++) for (int k=j+1; k<j+4 && k<len; k++) { if (isValid(s, 0, i) && isValid(s, i, j) && isValid(s, j, k) && isValid(s, k, len)) { string tmp = s.substr(0, i) + "." + s.substr(i, j-i) + "." + s.substr(j, k-j) + "." + s.substr(k, len-k); res.push_back(tmp); } } return res; } private: // check sub-string s[s, e)'s validity bool isValid(string &s, int a, int e) { //only 1 bit, true anyway if (a+1 == e) return true; int val = 0; bool leading = false; for (int i=a; i<e; i++) { if (leading) { val = val * 10 + s[i] - '0'; } else { if (s[i] == '0') return false; else { val = val * 10 + s[i]-'0'; leading = true; } } } if (val<=255 && val>=0) return true; else return false; } };
View Code
相关文章推荐
- [leetcode.com]算法题目 - Restore IP Addresses
- LeetCode Restore IP Addresses(回溯法)
- leetcode 093 —— Restore IP Addresses
- leetcode---Restore IP Addresses---回溯
- 【leetcode】Restore IP Addresses (middle)
- [LeetCode]题解(python):093-Restore IP Addresses
- Leetcode Restore IP Addresses
- LeetCode Restore IP Addresses
- leetcode[93] Restore IP Addresses
- leetcode-Restore IP Addresses-ZZ
- 【LeetCode】Restore IP Addresses 解题报告
- leetcode第一刷_Restore IP Addresses
- LeetCode:Restore IP Addresses
- leetcode:Restore IP Addresses (修复ip地址) 【面试算法题】
- leetcode-Restore IP Addresses
- LeetCode--restore-ip-addresses
- leetcode---restore-ip-addresses---dfs
- leetcode[93]Restore IP Addresses
- LeetCode - Restore IP Addresses
- LeetCode93 Restore IP Addresses