forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmintheon.java
More file actions
39 lines (29 loc) · 968 Bytes
/
mintheon.java
File metadata and controls
39 lines (29 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.ArrayList;
import java.util.List;
public class Codec {
String SPLIT = ":";
// Encodes a list of strings to a single string.
public String encode(List<String> strs) {
StringBuilder encoded = new StringBuilder();
for(String str : strs) {
encoded.append(str.length()).append(SPLIT).append(str);
}
return encoded.toString();
}
// Decodes a single string to a list of strings.
public List<String> decode(String s) {
List<String> decoded = new ArrayList<>();
int pointer = 0;
while(pointer < s.length()) {
int index = s.indexOf(SPLIT, pointer);
int length = Integer.parseInt(s.substring(pointer, index));
String str = s.substring(index + 1, index + 1 + length);
decoded.add(str);
pointer = index + 1 + length;
}
return decoded;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));