File tree Expand file tree Collapse file tree 1 file changed +15
-20
lines changed Expand file tree Collapse file tree 1 file changed +15
-20
lines changed Original file line number Diff line number Diff line change @@ -2,32 +2,27 @@ public class Solution {
2
2
3
3
public String encode (List <String > strs ) {
4
4
StringBuilder encodedString = new StringBuilder ();
5
- for (String str : strs ){
6
- int length = str .length ();
7
- encodedString .append (length + "#" );
8
- encodedString .append (str );
5
+ for (String str : strs ) {
6
+ encodedString . append ( str .length ())
7
+ .append ("#" )
8
+ .append (str );
9
9
}
10
10
return encodedString .toString ();
11
11
}
12
12
13
13
public List <String > decode (String str ) {
14
- List <String > decodedStrings = new ArrayList ();
15
- for (int i =0 ;i <str .length ();i ++){
16
- String length = "" ;
17
- while (str .charAt (i ) != '#' ){
18
- length += str .charAt (i );
19
- i ++;
20
- }
21
- int wordLength = Integer .parseInt (length );
22
- i ++;
23
14
24
- String word = "" ;
25
- for (int j =i ;j <wordLength +i ;j ++){
26
- word += str .charAt (j );
27
- }
28
- decodedStrings .add (word );
29
- i =i +wordLength -1 ;
15
+ List <String > list = new ArrayList <>();
16
+ int i = 0 ;
17
+ while (i < str .length ()) {
18
+ int j = i ;
19
+ while (str .charAt (j ) != '#' )
20
+ j ++;
21
+
22
+ int length = Integer .valueOf (str .substring (i , j ));
23
+ i = j + 1 + length ;
24
+ list .add (str .substring (j + 1 , i ));
30
25
}
31
- return decodedStrings ;
26
+ return list ;
32
27
}
33
28
}
You can’t perform that action at this time.
0 commit comments