Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc464c34

Browse files
author
rahulshishodia
authored
Update 271-Encode-and-Decode-Strings.java
1 parent51bcb14 commitc464c34

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

‎java/271-Encode-and-Decode-Strings.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,27 @@ public class Solution {
22

33
publicStringencode(List<String>strs) {
44
StringBuilderencodedString =newStringBuilder();
5-
for(Stringstr:strs){
6-
intlength =str.length();
7-
encodedString.append(length+"#");
8-
encodedString.append(str);
5+
for(Stringstr:strs){
6+
encodedString.append(str.length())
7+
.append("#")
8+
.append(str);
99
}
1010
returnencodedString.toString();
1111
}
1212

1313
publicList<String>decode(Stringstr) {
14-
List<String>decodedStrings =newArrayList();
15-
for(inti =0;i<str.length();i++){
16-
Stringlength ="";
17-
while(str.charAt(i) !='#'){
18-
length +=str.charAt(i);
19-
i++;
20-
}
21-
intwordLength =Integer.parseInt(length);
22-
i++;
2314

24-
Stringword ="";
25-
for(intj=i;j<wordLength+i;j++){
26-
word +=str.charAt(j);
27-
}
28-
decodedStrings.add(word);
29-
i=i+wordLength-1;
15+
List<String>list =newArrayList<>();
16+
inti =0;
17+
while (i <str.length()) {
18+
intj =i;
19+
while (str.charAt(j) !='#')
20+
j++;
21+
22+
intlength =Integer.valueOf(str.substring(i,j));
23+
i =j +1 +length;
24+
list.add(str.substring(j +1,i));
3025
}
31-
returndecodedStrings;
26+
returnlist;
3227
}
3328
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp