forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimsosleepy.java
More file actions
61 lines (48 loc) ยท 1.8 KB
/
imsosleepy.java
File metadata and controls
61 lines (48 loc) ยท 1.8 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// ์์ด๋์ด๋ก ํธ๋ ๋ฌธ์ ๋ผ ์ ํธํ์ง ์๋ ๋ฌธ์ ...
// ๊ทธ๋ฅ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ ๊ตฌ๋ถ์๋ก ๋๊ณ ์คํ๋ฆฟํ๋๊ฒ ๊ฐ์ฅ ํธํ๋ค. ์์ ๋์ค์ง ์์ ๋ฌธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋๋ฉด ๊ธธ์ด๋ฅผ ์ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ
public class Solution {
// ์ธ์ฝ๋ฉ ๋ฉ์๋
public String encode(List<String> strs) {
StringBuilder encodedString = new StringBuilder();
for (String str : strs) {
encodedString.append(str.length()).append("#").append(str);
}
return encodedString.toString();
}
// ๋์ฝ๋ฉ ๋ฉ์๋
public List<String> decode(String s) {
List<String> decodedList = new ArrayList<>();
int i = 0;
while (i < s.length()) {
int j = i;
while (s.charAt(j) != '#') {
j++;
}
int length = Integer.parseInt(s.substring(i, j));
decodedList.add(s.substring(j + 1, j + 1 + length));
i = j + 1 + length;
}
return decodedList;
}
}
// ๐๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ๋ถ๋ฆฌ
// @!#$@#$ ์ด๋ฐ๊ฑธ ์คํ๋ฆฟ ๋ฌธ์๋ก ๋๋ ๋ฐฉ๋ฒ๋ ์๋ค.์์ด์จ
public class Solution {
public String encode(List<String> strs) {
StringBuilder encodedString = new StringBuilder();
for (String str : strs) {
encodedString.append(str).append("๐");
}
return encodedString.toString();
}
public List<String> decode(String s) {
String[] parts = s.split("๐");
List<String> decodedList = new ArrayList<>();
for (String part : parts) {
if (!part.isEmpty()) {
decodedList.add(part);
}
}
return decodedList;
}
}