forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsora0319.java
More file actions
33 lines (32 loc) · 943 Bytes
/
sora0319.java
File metadata and controls
33 lines (32 loc) · 943 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
public class Solution {
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
public String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();
for (String s : strs) {
sb.append(s.length()).append('#').append(s);
}
return sb.toString();
}
/*
* @param str: A single encoded string
* @return: decodes the single string to a list of strings
*/
public List<String> decode(String str) {
List<String> result = new ArrayList<>();
int i = 0;
while (i < str.length()) {
int j = i;
while (str.charAt(j) != '#') {
j++;
}
int length = Integer.parseInt(str.substring(i, j));
j++;
result.add(str.substring(j, j + length));
i = j + length;
}
return result;
}
}