LeetCode93: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)

思路:搜索DFS+剪枝

剪枝策略:IP为4个子串,递归4层即可。

 1 public class Solution {
 2    public List<String> restoreIpAddresses(String s) {
 3             List<String> ans=new ArrayList<String>();
 4             if(s.length()==0) return ans;
 5             dfs(ans,s,0,"",1);
 6             return ans;
 7         }
 8
 9      void dfs(List<String> ans, String s, int start, String string, int index) {
10         // TODO Auto-generated method stub
11          if(start>=s.length()){
12              return ;
13          }
14          if(index==4){
15              String sub=s.substring(start);
16              if(isValid(sub)){
17                  ans.add(string+"."+sub);
18              }
19              return;
20          }
21         for(int i=1;i<4&&((start+i)<=s.length());i++){
22             String sub=s.substring(start,start+i);
23             if(isValid(sub)){
24                 if(index==1){
25                     dfs(ans, s, start+i, sub, index+1);
26                 }else{
27                     dfs(ans, s, start+i, string+"."+sub, index+1);
28                 }
29             }
30         }
31     }
32
33     private boolean isValid(String sub) {
34         // TODO Auto-generated method stub
35         if(sub.length()>=4) return false;
36         if(sub.charAt(0)==‘0‘&&sub.length()>1) return false;
37         int temp=Integer.parseInt(sub);
38         if(temp>=0&&temp<=255){
39             return true;
40         }
41         return false;
42     }
43 }

参考:http://blog.csdn.net/yangliuy/article/details/43915517

时间: 2024-10-13 07:36:24

LeetCode93:Restore IP Addresses的相关文章

LeetCode93——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) 难度系数:中 实现 vecto

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题解(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

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

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

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) 题解: 利用循环递归解

【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