|
1 | | -package Solution; |
2 | | - |
3 | | -import java.util.ArrayList; |
4 | | - |
5 | | -public class RestoreIPAddresses { |
6 | | - |
7 | | - /** |
8 | | - * @param args |
9 | | - */ |
10 | | - public static void main(String[] args) { |
11 | | - // TODO Auto-generated method stub |
12 | | - RestoreIPAddresses instance=new RestoreIPAddresses(); |
13 | | - System.out.println(instance.restoreIpAddresses("010010")); |
14 | | - } |
15 | | - |
16 | | - public ArrayList<String> restoreIpAddresses(String s) { |
17 | | - // Start typing your Java solution below |
18 | | - // DO NOT write main() function |
19 | | - // we will use recursion, however, parse the string in triple |
20 | | - ArrayList<String> result=new ArrayList<String>(); |
21 | | - // obviously illegal case |
22 | | - if (s==null || s.length()<4 || s.length()>12) |
| 1 | +public class Solution { |
| 2 | + public ArrayList<String> restoreIpAddresses(String s) { |
| 3 | + // IMPORTANT: Please reset any member data you declared, as |
| 4 | + // the same Solution instance will be reused for each test case. |
| 5 | + // we will use backtrace |
| 6 | + if (s==null || s.length()<4) |
23 | 7 | { |
24 | | - return result; |
| 8 | + return new ArrayList<String>(); |
25 | 9 | } |
26 | | - int[] cuts=new int[3]; |
27 | | - cuts[0]=1; |
28 | | - result.addAll(restoreIpAddresses(s, cuts, 1)); |
29 | | - cuts[0]=2; |
30 | | - result.addAll(restoreIpAddresses(s, cuts, 1)); |
31 | | - cuts[0]=3; |
32 | | - result.addAll(restoreIpAddresses(s, cuts, 1)); |
33 | | - return result; |
| 10 | + return restoreIpAddresses(s, 0, new ArrayList<Integer>()); |
34 | 11 | } |
35 | 12 |
|
36 | | - // start indicates the string we are parsing |
37 | | - // cuts for the current cutting position (the start of each segment) |
38 | | - // n indicates the number of segements already obtained |
39 | | - private ArrayList<String> restoreIpAddresses(String s, int[] cuts, int n) |
| 13 | + /** |
| 14 | + * @param start is the index of char we can use |
| 15 | + */ |
| 16 | + private ArrayList<String> restoreIpAddresses(String s, int start, ArrayList<Integer> base) |
40 | 17 | { |
41 | 18 | ArrayList<String> result=new ArrayList<String>(); |
42 | | - // we first check the string are good so far |
43 | | - if (cuts[n-1]>=s.length()) |
44 | | - { |
45 | | - return result; |
46 | | - } |
47 | | - // first we will check the previous section is valid or not |
48 | | - long val; |
49 | | - if (n==1) |
| 19 | + // we finish the string |
| 20 | + if (start>=s.length()) |
50 | 21 | { |
51 | | - if (!validSegment(s, 0, cuts[0])) |
| 22 | + if (base.size()==4) |
52 | 23 | { |
53 | | - return result; |
| 24 | + StringBuffer str=new StringBuffer(); |
| 25 | + str.append(base.get(0)); |
| 26 | + str.append('.'); |
| 27 | + str.append(base.get(1)); |
| 28 | + str.append('.'); |
| 29 | + str.append(base.get(2)); |
| 30 | + str.append('.'); |
| 31 | + str.append(base.get(3)); |
| 32 | + result.add(str.toString()); |
54 | 33 | } |
55 | | - val=Long.parseLong(s.substring(0, cuts[0])); |
56 | 34 | } |
57 | | - else |
| 35 | + // the string is not finished yet and the ip address is not complete yet |
| 36 | + // however, we need to avoid '00' of something similar |
| 37 | + else if(base.size()<4) |
58 | 38 | { |
59 | | - if (!validSegment(s, cuts[n-2], cuts[n-1])) |
| 39 | + ArrayList<Integer> entry=null; |
| 40 | + // we can have triple here |
| 41 | + // 1 the string is not long enough |
| 42 | + // 2 the string has value >255 |
| 43 | + // 3 the string has something like 0xx |
| 44 | + if (start+2<s.length() && (s.charAt(start)-'0')*100+(s.charAt(start+1)-'0')*10+(s.charAt(start+2)-'0')<=255 && (s.charAt(start)-'0')*100+(s.charAt(start+1)-'0')*10+(s.charAt(start+2)-'0')>99) |
60 | 45 | { |
61 | | - return result; |
| 46 | + // add this single char |
| 47 | + entry=new ArrayList<Integer>(base); |
| 48 | + entry.add(s.charAt(start)-'0'); |
| 49 | + result.addAll(restoreIpAddresses(s, start+1, entry)); |
| 50 | + // also check the two char |
| 51 | + entry=new ArrayList<Integer>(base); |
| 52 | + entry.add((s.charAt(start)-'0')*10+(s.charAt(start+1)-'0')); |
| 53 | + result.addAll(restoreIpAddresses(s, start+2, entry)); |
| 54 | + // and triple |
| 55 | + entry=new ArrayList<Integer>(base); |
| 56 | + entry.add((s.charAt(start)-'0')*100+(s.charAt(start+1)-'0')*10+(s.charAt(start+2)-'0')); |
| 57 | + result.addAll(restoreIpAddresses(s, start+3, entry)); |
62 | 58 | } |
63 | | - val=Long.parseLong(s.substring(cuts[n-2], cuts[n-1])); |
64 | | - } |
65 | | - // this is not a valid segment |
66 | | - if (val>255 || val<0) |
67 | | - { |
68 | | - return result; |
69 | | - } |
70 | | - // everything is good so far |
71 | | - if (n==3) |
72 | | - { |
73 | | - // we have finished the cutting, but need to check the last segment |
74 | | - if (!validSegment(s, cuts[n-1], s.length())) |
| 59 | + // we can have double |
| 60 | + // 1 the string is not long enough |
| 61 | + // 2 the string is something like 0x |
| 62 | + else if(start+1<s.length() && (s.charAt(start)-'0')*10+(s.charAt(start+1)-'0')>9) |
75 | 63 | { |
76 | | - return result; |
77 | | - } |
78 | | - val=Integer.parseInt(s.substring(cuts[n-1], s.length())); |
79 | | - if (val>255 || val<0) |
80 | | - { |
81 | | - return result; |
| 64 | + // add this single char |
| 65 | + entry=new ArrayList<Integer>(base); |
| 66 | + entry.add(s.charAt(start)-'0'); |
| 67 | + result.addAll(restoreIpAddresses(s, start+1, entry)); |
| 68 | + // also check the two char |
| 69 | + entry=new ArrayList<Integer>(base); |
| 70 | + entry.add((s.charAt(start)-'0')*10+(s.charAt(start+1)-'0')); |
| 71 | + result.addAll(restoreIpAddresses(s, start+2, entry)); |
82 | 72 | } |
| 73 | + // only single |
83 | 74 | else |
84 | 75 | { |
85 | | - result.add(string2IP(s, cuts)); |
86 | | - return result; |
| 76 | + // add this single char |
| 77 | + entry=new ArrayList<Integer>(base); |
| 78 | + entry.add(s.charAt(start)-'0'); |
| 79 | + result.addAll(restoreIpAddresses(s, start+1, entry)); |
87 | 80 | } |
88 | 81 | } |
89 | | - else |
90 | | - { |
91 | | - // just found the possible cutting location |
92 | | - for (int i=cuts[n-1]+1; i<s.length(); i++) |
93 | | - { |
94 | | - cuts[n]=i; |
95 | | - result.addAll(restoreIpAddresses(s, cuts, n+1)); |
96 | | - cuts[n]=0; |
97 | | - } |
98 | | - return result; |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - private boolean validSegment(String s, int start, int end) |
103 | | - { |
104 | | - if (start==end-1) |
105 | | - { |
106 | | - return true; |
107 | | - } |
108 | | - else |
109 | | - { |
110 | | - return s.charAt(start)!='0'; |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - private String string2IP(String s, int[]cuts) |
115 | | - { |
116 | | - StringBuffer result=new StringBuffer(); |
117 | | - result.append(s.substring(0, cuts[0])); |
118 | | - result.append('.'); |
119 | | - result.append(s.substring(cuts[0], cuts[1])); |
120 | | - result.append('.'); |
121 | | - result.append(s.substring(cuts[1], cuts[2])); |
122 | | - result.append('.'); |
123 | | - result.append(s.substring(cuts[2], s.length())); |
124 | | - return result.toString(); |
| 82 | + return result; |
125 | 83 | } |
126 | 84 | } |
0 commit comments