**一. 题目描述**
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)
**二. 题目分析**
题目的大意是,输入一个仅含整数的字符串,找出所有合法的ip地址,并输出所有的可能性。
很显然这道题需要遍历到最后一个字符才能判断一个解是否合法,因此需使用dfs。本题的难点在于存在各种各样的限制条件,如每段中不能出现以0开头的数字。需反复尝试方可AC,由于时间有限,给出一个参考的代码链接,后续需深入研究。
**三. 示例代码**
~~~
class Solution
{
public:
vector<string> restore(string s, int n, bool &flag)
{
vector<string> res;
if(s.length() < n) {flag = false; return res;}
if(n==1)
{
if(s.length()>3){flag=false;return res;}//maybe larger than int
int ip = stoi(s);
if(ip==0 && s.length() >1) flag = false;
else if(ip>0 && s[0] == '0') flag = false;
else if((ip >= 0) && (ip < 256))
{
res.push_back(s);
flag = true;
}else
flag = false;
return res;
}
int loop = s.length() - n +1;//garuntee every ip address is no less than one bit
loop = min(loop,3);
for(int i = 1; i <= loop; i++)
{
string ipstr = s.substr(0,i);
int ip = stoi(ipstr);
if(ip==0 && ipstr.length() >1)break;
if(ip>0 && ipstr[0] == '0')break;
if((ip>=0) && (ip < 256))
{
string suffix = s.substr(i);
bool f = false;
vector<string> tmp = restore(suffix,n-1,f);
if(f)
{
flag = true;
for(int k = 0; k < tmp.size(); k++)
res.push_back(ipstr + '.' + tmp[k]);
}
}
}
return res;
}
vector<string> restoreIpAddresses(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> res;
bool flag = false;
res = restore(s,4,flag);
return res;
}
}
~~~
**四. 小结**
递归到最后一个数并进行判断,最后输出结果。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching