LeetCode: Restore IP Addresses 解题报告

Restore IP Addresses

My Submissions

Question

 Solution 

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)

SOLUTION 1:

如果有跟过主页君的讲解,就会知道,这又是一道相当经典的DFS模板题目。 我们照样套用http://www.ninechapter.com/的递归

模板,退出条件是:path.size == 4.

在模板中,我们加入一些判断条件来中断循环:例如说判断pre的字符串转化后的数字是不是在255以内。

另外,我们要排除055这种数字,所以加入这一行判断:

if (s.charAt(index) == '0' && i > index) {
  break;
}

虽然是很简单的递归题,但主页君是真心用心写了的。而且这是相当经典的递归模板。同学们一定要记住这种模板解法哦!

 1 public List<String> restoreIpAddresses(String s) {
 2         if (s == null) {
 3             return null;
 4         }
 5         
 6         ArrayList<String> ret = new ArrayList<String>();
 7         ArrayList<String> path = new ArrayList<String>();
 8         
 9         dfs(s, 0, path, ret);
10         
11         return ret;
12     }
13     
14     public void dfs(String s, int index, ArrayList<String> path, ArrayList<String> ret) {
15         if (path.size() == 4) {
16             if (index == s.length()) {
17                 StringBuilder sb = new StringBuilder();
18                 for (String str: path) {
19                     sb.append(str);
20                     sb.append('.');
21                 }
22                 
23                 sb.deleteCharAt(sb.length() - 1);
24                 ret.add(sb.toString());
25             }
26             
27             return;
28         }
29         
30         int len = s.length();
31         for (int i = index; i < index + 3 && i < len; i++) {
32             if (s.charAt(index) == '0' && i > index) {
33                 break;
34             }
35             
36             String pre = s.substring(index, i + 1);
37             int num = Integer.parseInt(pre);
38             if (num > 255) {
39                 continue;
40             }
41             
42             path.add(pre);
43             dfs(s, i + 1, path, ret);
44             path.remove(path.size() - 1);
45         }
46     }
View Code

2015.1.1 redo:

容易出错的点:1. i的索引,注意不要越界。2. 记得把sb添加到ret中。

 1 public class Solution {
 2     public List<String> restoreIpAddresses(String s) {
 3         List<String> ret = new ArrayList<String>();
 4         // Bug 1: not length, but length().
 5         if (s == null || s.length() < 4 || s.length() > 12) {
 6             return ret;
 7         }
 8         
 9         dfs(s, new ArrayList<String>(), ret, 0);
10         return ret;
11     }
12     
13     public void dfs(String s, List<String> path, List<String> ret, int index) {
14         // THE BASE CASE:
15         int len = s.length();
16         if (path.size() == 4) {
17             // Create a solution.
18             if (index == len) {
19                 StringBuilder sb = new StringBuilder();
20                 for (String str: path) {
21                     sb.append(str);
22                     sb.append(".");
23                 }
24                 sb.deleteCharAt(sb.length() - 1);
25                 
26                 // bug 3: forget this statement.
27                 ret.add(sb.toString());
28             }
29             
30             return;
31         }
32         
33         // 2/ 25 / 255
34         // bug 2: i should < len.
35         for (int i = index; i < index + 3 && i < len; i++) {
36             String sub = s.substring(index, i + 1);
37             if (s.charAt(index) == '0' && i != index) {
38                 // only allow 0, not 02, 022
39                 break;
40             }
41             
42             if (!isValid(sub)) {
43                 continue;
44             }
45             
46             path.add(sub);
47             dfs(s, path, ret, i + 1);
48             path.remove(path.size() - 1);
49         }
50     }
51     
52     public boolean isValid(String s) {
53         int num = Integer.parseInt(s);
54         return num >= 0 && num <= 255;
55     }
56 }
View Code

 

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/RestoreIpAddresses.java

posted on 2014-11-18 20:35  Yu's Garden  阅读(1964)  评论(2编辑  收藏  举报

导航