Skip to content

Commit 9b5a79e

Browse files
[LEET-93] add 93
1 parent c553ded commit 9b5a79e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
|96|[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/UniqueBinarySearchTrees.java) | O(n^2) | O(n) | Medium | Recursion, DP
260260
|95|[Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/UniqueBinarySearchTreesII.java) | O(?) | O(?) | Medium | Recursion
261261
|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryTreeInorderTraversal.java)| O(n)|O(h) | Medium| Binary Tree
262+
|93|[Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RestoreIPAddresses.java)| O(1)|O(1) | Medium | Backtracking
262263
|92|[Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ReverseLinkedListII.java)| O(n)|O(1) | Medium
263264
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DecodeWays.java)| O(n)|O(n) | Medium| DP
264265
|89|[Gray Code](https://leetcode.com/problems/gray-code/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/GrayCode.java)|O(n) |O(1)|Medium|Bit Manipulation
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Given a string containing only digits, restore it by returning all possible valid IP address combinations.
8+
9+
For example:
10+
Given "25525511135",
11+
12+
return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
13+
*/
14+
public class RestoreIPAddresses {
15+
16+
public List<String> restoreIpAddresses(String s) {
17+
List<String> allValidIpAddresses = new ArrayList<>();
18+
if (s == null || s.length() > 12 || s.length() < 4) return allValidIpAddresses;
19+
backtracking(s, new ArrayList<>(), allValidIpAddresses, 0);
20+
return allValidIpAddresses;
21+
}
22+
23+
private void backtracking(String s, ArrayList<String> bytes, List<String> result, int pos) {
24+
if (bytes.size() == 4) {
25+
if (pos != s.length()) return;
26+
StringBuilder stringBuilder = new StringBuilder();
27+
for (int i = 0; i < 4; i++) {
28+
stringBuilder.append(bytes.get(i));
29+
stringBuilder.append(".");
30+
}
31+
stringBuilder.setLength(stringBuilder.length()-1);
32+
result.add(stringBuilder.toString());
33+
return;
34+
}
35+
36+
for (int i = pos; i < pos+4 && i < s.length(); i++) {
37+
String oneByte = s.substring(pos, i+1);
38+
if (!isValid(oneByte)) continue;
39+
bytes.add(oneByte);
40+
backtracking(s, bytes, result, i+1);
41+
bytes.remove(bytes.size()-1);
42+
}
43+
}
44+
45+
private boolean isValid(String oneByte) {
46+
if (oneByte.charAt(0) == '0') return oneByte.equals("0");
47+
int num = Integer.valueOf(oneByte);
48+
return (num >= 0 && num < 256);
49+
}
50+
51+
52+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.RestoreIPAddresses;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import static junit.framework.Assert.assertEquals;
12+
13+
public class RestoreIPAddressesTest {
14+
private static RestoreIPAddresses test;
15+
private static List<String> expected;
16+
private static List<String> actual;
17+
private static String s;
18+
19+
@BeforeClass
20+
public static void setup(){
21+
test = new RestoreIPAddresses();
22+
}
23+
24+
@Before
25+
public void setupForEachTest(){
26+
expected = new ArrayList<>();
27+
actual = new ArrayList<>();
28+
}
29+
30+
@Test
31+
public void test1(){
32+
s = "25525511135";
33+
expected.add("255.255.11.135");
34+
expected.add("255.255.111.35");
35+
actual = test.restoreIpAddresses(s);
36+
assertEquals(expected, actual);
37+
}
38+
}

0 commit comments

Comments
 (0)