File tree 1 file changed +8
-6
lines changed
scripts/algorithms/R/Restore IP Addresses
1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 15 ms (Top 19.20%) | Memory: 43.5 MB (Top 45.49%)
2
+
1
3
class Solution {
2
4
public List <String > restoreIpAddresses (String s ) {
3
5
List <String > ans = new ArrayList <>();
4
-
6
+
5
7
int len = s .length ();
6
8
for (int i = 1 ; i < 4 && i < len -2 ; i ++ ){
7
9
for (int j = i + 1 ; j < i + 4 && j < len -1 ; j ++ ){
8
10
for (int k = j +1 ; k < j + 4 && k < len ; k ++){
9
11
String s1 = s .substring (0 ,i );
10
- String s2 = s .substring (i ,j );
11
- String s3 = s .substring (j ,k );
12
- String s4 = s .substring (k ,len );
12
+ String s2 = s .substring (i ,j );
13
+ String s3 = s .substring (j ,k );
14
+ String s4 = s .substring (k ,len );
13
15
if (isValid (s1 ) && isValid (s2 ) && isValid (s3 ) && isValid (s4 ))
14
- ans .add (s1 +"." +s2 +"." +s3 +"." +s4 );
16
+ ans .add (s1 +"." +s2 +"." +s3 +"." +s4 );
15
17
}
16
18
}
17
19
}
@@ -22,4 +24,4 @@ boolean isValid(String s){
22
24
return false ;
23
25
return true ;
24
26
}
25
- }
27
+ }
You can’t perform that action at this time.
0 commit comments