Restore IP Addresses leetcode java

题目

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)

题解

利用循环递归解决子问题。对于每个段内数来说,最多3位最少1位,所以在每一层可以循环3次,来尝试填段。因为IP地址最多4个分段,当层数是3的时候说明已经尝试填过3个段了,那么把剩余没填的数段接到结尾即可。

这个过程中要保证的是填的数是合法的,最后拼接的剩余的数也是合法的。

注意开头如果是0的话要特殊处理,如果开头是0,判断整个串是不是0,不是的话该字符就是非法的。因为001,01都是不对的。

代码如下:

1     public ArrayList<String> restoreIpAddresses(String s) {  
 2         ArrayList<String> res = new ArrayList<String>();  
 3         String item = new String();
 4         if (s.length()<4||s.length()>12) 
 5         return res;  
 6         
 7         dfs(s, 0, item, res);  
 8         return res;  
 9     }  
10       
11     public void dfs(String s, int start, String item, ArrayList<String> res){  
12         if (start == 3 && isValid(s)) {  
13             res.add(item + s);  
14             return;  
15         }  
16         for(int i=0; i<3 && i<s.length()-1; i++){  
17             String substr = s.substring(0,i+1);  
18             if (isValid(substr))
19                 dfs(s.substring(i+1, s.length()), start+1, item + substr + ‘.‘, res);  
20         }  
21     }  
22       
23     public boolean isValid(String s){  
24         if (s.charAt(0)==‘0‘)
25             return s.equals("0");  
26             int num = Integer.parseInt(s);
27             
28         if(num <= 255 && num > 0)
29             return true;
30         else
31             return false;
32     }

Refrence:http://blog.csdn.net/u011095253/article/details/9158449

Restore IP Addresses leetcode java

时间: 2024-10-13 00:42:34

Restore IP Addresses leetcode java的相关文章

Restore IP Addresses [leetcode]

这题乍一看有点像Decode Ways,实际上是一个深搜+剪枝的题目 也可以通过三个for循环寻找可行的'.'的位置 递归方法如下: vector<string> restoreIpAddresses(string s) { vector<string> res; restore(s, 0, 0, res, ""); return res; } void restore(string & s, int start, int ipNum, vector&l

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

[LeetCode]Restore IP Addresses

题目:Restore IP Addresses 将数字串转换成合法的IP地址,返回所有可能的情况. 思路: 移动3个地址分隔符".",将地址换分成四份,检查是否合法: 注意不能有0开头的地址段(它是非法的),且不用将开头的0去掉. 中间的点从第一个点的后面开始向尾部移动,当第一个点与第二个点的距离大于3时,第一个点向后移动一步,第二个点还原到当前第一个点的后面: 当第一个点移动到最后一个点的前2个位置,则,最后一个点向前移动一步,第一个点和第二个点还原: 最后一个点到串尾距离大于3则退

【leetcode刷题笔记】Restore IP Addresses

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) 题解:深度优先搜索.用resul

LeetCode: Restore IP Addresses [093]

[题目] 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) [题意] 给定一个字

Leetcode题解(2):L93/Restore IP Addresses

L93: Restore IP Addresses 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 n

【leetcode】Restore IP Addresses

Restore IP Addresses 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 mat

【LeetCode】93. Restore IP Addresses 【面试题】

Restore IP Addresses 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 mat

93. Restore IP Addresses

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) 什么时候临时容器符合题意加入结