8000 refactor 468 · hemantku/Leetcode@89c3a44 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89c3a44

Browse files
refactor 468
1 parent 64944da commit 89c3a44

File tree

2 files changed

+95
-102
lines changed

2 files changed

+95
-102
lines changed

src/main/java/com/fishercoder/solutions/_468.java

Lines changed: 80 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
/**
88
* 468. Validate IP Address
9+
*
910
* Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.
10-
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers,
11-
each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
12-
Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.
11+
* IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers,
12+
* each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
13+
* Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.
1314
1415
IPv6 addresses are represented as eight groups of four hexadecimal digits,
1516
each group representing 16 bits.
@@ -29,128 +30,120 @@ group using two consecutive colons (::) to pursue simplicity.
2930
Note: You may assume there is no extra space or special characters in the input string.
3031
3132
Example 1:
32-
3333
Input: "172.16.254.1"
34-
3534
Output: "IPv4"
36-
3735
Explanation: This is a valid IPv4 address, return "IPv4".
3836
3937
Example 2:
40-
4138
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334"
42-
4339
Output: "IPv6"
44-
4540
Explanation: This is a valid IPv6 address, return "IPv6".
4641
4742
Example 3:
48-
4943
Input: "256.256.256.256"
50-
5144
Output: "Neither"
52-
5345
Explanation: This is neither a IPv4 address nor a IPv6 address.
54-
55-
*/
46+
validIPAddress */
5647
public class _468 {
5748

58-
static final String NEITHER = "Neither";
49+
public static class Solution1 {
5950

60-
public String validIPAddress(String IP) {
61-
if (IP.contains(".")) {
62-
return isValidIPv4(IP);
63-
} else if (IP.contains(":")) {
64-
return isValidIPv6(IP);
65-
} else {
66-
return NEITHER;
67-
}
68-
}
51+
static final String NEITHER = "Neither";
6952

70-
private String isValidIPv6(String IP) {
71-
if (getDelimiterCount(IP, ':') != 7) {
72-
return NEITHER;
73-
}
74-
String[] bytes = IP.split("\\:");
75-
if (bytes.length != 8) {
76-
return NEITHER;
53+
public String validIPAddress(String IP) {
54+
if (IP.contains(".")) {
55+
return isValidIPv4(IP);
56+
} else if (IP.contains(":")) {
57+
return isValidIPv6(IP);
58+
} else {
59+
return NEITHER;
60+
}
7761
}
78-
for (int i = 0; i < 8; i++) {
79-
if (hasInvalidIPV6Char(bytes[i])) {
62+
63+
private String isValidIPv6(String IP) {
64+
if (getDelimiterCount(IP, ':') != 7) {
8065
return NEITHER;
8166
}
82-
try {
83-
if (bytes[i].length() > 4) {
84-
return NEITHER;
85-
}
86-
int intNum = Integer.parseInt(bytes[i], 16);
87-
if (intNum < 0) {
67+
String[] bytes = IP.split("\\:");
68+
if (bytes.length != 8) {
69+
return NEITHER;
70+
}
71+
for (int i = 0; i < 8; i++) {
72+
if (hasInvalidIPV6Char(bytes[i])) {
8873
return NEITHER;
8974
}
90-
if (i == 0 && intNum != 0 && bytes[i].charAt(0) == '0') {
75+
try {
76+
if (bytes[i].length() > 4) {
77+
return NEITHER;
78+
}
79+
int intNum = Integer.parseInt(bytes[i], 16);
80+
if (intNum < 0) {
81+
return NEITHER;
82+
}
83+
if (i == 0 && intNum != 0 && bytes[i].charAt(0) == '0') {
84+
return NEITHER;
85+
}
86+
} catch (Exception e) {
9187
return NEITHER;
9288
}
93-
} catch (Exception e) {
94-
return NEITHER;
95-
}
9689

90+
}
91+
return "IPv6";
9792
}
98-
return "IPv6";
99-
}
10093

101-
private String isValidIPv4(String IP) {
102-
if (getDelimiterCount(IP, '.') != 3) {
103-
return NEITHER;
104-
}
105-
String[] bytes = IP.split("\\.");
106-
if (bytes.length != 4) {
107-
return NEITHER;
108-
}
109-
for (String num : bytes) {
110-
try {
111-
int intNum = Integer.parseInt(num);
112-
if (intNum > 255 || intNum < 0) {
113-
return NEITHER;
114-
}
115-
if (intNum != 0 ) {
116-
for (int i = 0; i < num.length(); i++) {
117-
if (num.charAt(i) == '0') {
94+
private String isValidIPv4(String IP) {
95+
if (getDelimiterCount(IP, '.') != 3) {
96+
return NEITHER;
97+
}
98+
String[] bytes = IP.split("\\.");
99+
if (bytes.length != 4) {
100+
return NEITHER;
101+
}
102+
for (String num : bytes) {
103+
try {
104+
int intNum = Integer.parseInt(num);
105+
if (intNum > 255 || intNum < 0) {
106+
return NEITHER;
107+
}
108+
if (intNum != 0) {
109+
for (int i = 0; i < num.length(); i++) {
110+
if (num.charAt(i) == '0') {
111+
return NEITHER;
112+
} else {
113+
break;
114+
}
115+
}
116+
} else if (intNum == 0) {
117+
if (num.length() != 1) {
118118
return NEITHER;
119-
} else {
120-
break;
121119
}
122120
}
123-
} else if (intNum == 0) {
124-
if (num.length() != 1) {
125-
return NEITHER;
126-
}
121+
} catch (Exception e) {
122+
return NEITHER;
127123
}
128-
} catch (Exception e) {
129-
return NEITHER;
130124
}
125+
return "IPv4";
131126
}
132-
return "IPv4";
133-
}
134127

135-
private boolean hasInvalidIPV6Char(String str) {
136-
Set<Character> set = new HashSet<>(Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
137-
'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'));
138-
for (char c : str.toCharArray()) {
139-
if (!set.contains(c)) {
140-
return true;
128+
private boolean hasInvalidIPV6Char(String str) {
129+
Set<Character> set = new HashSet<>(Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
130+
'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'));
131+
for (char c : str.toCharArray()) {
132+
if (!set.contains(c)) {
133+
return true;
134+
}
141135
}
136+
return false;
142137
}
143-
return false;
144-
}
145138

146-
private int getDelimiterCount(String ip, char delimiter) {
147-
int count = 0;
148-
for (char c : ip.toCharArray()) {
149-
if (c == delimiter) {
150-
count++;
139+
private int getDelimiterCount(String ip, char delimiter) {
140+
int count = 0;
141+
for (char c : ip.toCharArray()) {
142+
if (c == delimiter) {
143+
count++;
144+
}
151145
}
146+
return count;
152147
}
153-
return count;
154148
}
155-
156149
}

src/test/java/com/fishercoder/_468Test.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,76 @@
1010
* Created by stevesun on 6/10/17.
1111
*/
1212
public class _468Test {
13-
private static _468 test;
13+
private static _468.Solution1 solution1;
1414

1515
@BeforeClass
1616
public static void setup() {
17-
test = new _468();
17+
solution1 = new _468.Solution1();
1818
}
1919

2020
@Test
2121
public void test1() {
22-
assertEquals("IPv4", test.validIPAddress("172.16.254.1"));
22+
assertEquals("IPv4", solution1.validIPAddress("172.16.254.1"));
2323
}
2424

2525
@Test
2626
public void test2() {
27-
assertEquals("IPv6", test.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334"));
27+
assertEquals("IPv6", solution1.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334"));
2828
}
2929

3030
@Test
3131
public void test3() {
32-
assertEquals("Neither", test.validIPAddress("2001:0db8:85a3::8A2E:0370:7334"));
32+
assertEquals("Neither", solution1.validIPAddress("2001:0db8:85a3::8A2E:0370:7334"));
3333
}
3434

3535
@Test
3636
public void test4() {
37-
assertEquals("Neither", test.validIPAddress("02001:0db8:85a3:0000:0000:8a2e:0370:7334"));
37+
assertEquals("Neither", solution1.validIPAddress("02001:0db8:85a3:0000:0000:8a2e:0370:7334"));
3838
}
3939

4040
@Test
4141
public void test5() {
42-
assertEquals("Neither", test.validIPAddress("256.256.256.256"));
42+
assertEquals("Neither", solution1.validIPAddress("256.256.256.256"));
4343
}
4444

4545
@Test
4646
public void test6() {
47-
assertEquals("Neither", test.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334:"));
47+
assertEquals("Neither", solution1.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334:"));
4848
}
4949

5050
@Test
5151
public void test7() {
52-
assertEquals("Neither", test.validIPAddress("01.01.01.01"));
52+
assertEquals("Neither", solution1.validIPAddress("01.01.01.01"));
5353
}
5454

5555
@Test
5656
public void test8() {
57-
assertEquals("Neither", test.validIPAddress("00.0.0.0"));
57+
assertEquals("Neither", solution1.validIPAddress("00.0.0.0"));
5858
}
5959

6060
@Test
6161
public void test9() {
62-
assertEquals("Neither", test.validIPAddress("000.0.0.0"));
62+
assertEquals("Neither", solution1.validIPAddress("000.0.0.0"));
6363
}
6464

6565
@Test
6666
public void test10() {
67-
assertEquals("Neither", test.validIPAddress("0000.0.0.0"));
67+
assertEquals("Neither", solution1.validIPAddress("0000.0.0.0"));
6868
}
6969

7070
@Test
7171
public void test11() {
72-
assertEquals("IPv4", test.validIPAddress("0.0.0.0"));
72+
assertEquals("IPv4", solution1.validIPAddress("0.0.0.0"));
7373
}
7474

7575
@Test
7676
public void test12() {
77-
assertEquals("Neither", test.validIPAddress("1081:db8:85a3:01:-0:8A2E:0370:7334"));
77+
assertEquals("Neither", solution1.validIPAddress("1081:db8:85a3:01:-0:8A2E:0370:7334"));
7878
}
7979

8080
@Test
8181
public void test13() {
82-
assertEquals("Neither", test.validIPAddress("1081:db8:85a3:01:z:8A2E:0370:7334"));
82+
assertEquals("Neither", solution1.validIPAddress("1081:db8:85a3:01:z:8A2E:0370:7334"));
8383
}
8484

8585
}

0 commit comments

Comments
 (0)
0