forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaurus09318976.py
More file actions
47 lines (38 loc) ยท 1.85 KB
/
taurus09318976.py
File metadata and controls
47 lines (38 loc) ยท 1.85 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
'''
๋ชฉํ : ๋ฌธ์์ด ๋ฆฌ์คํธ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํ๊ณ , ๋ค์ ์๋ ๋ฆฌ์คํธ๋ก ๋์ฝ๋ฉํ๋ ์๊ณ ๋ฆฌ๋ฌ์ ๋ง๋๋ ๋ฌธ์ ์
ํต์ฌ : ๋ฌธ์์ด์ ์ด๋ค ๋ฌธ์๊ฐ ๋ค์ด ์๋๋ผ๋ ์ ํํ ์ธ์ฝ๋ฉ/๋์ฝ๋ฉํ ์ ์์ด์ผ ํจ
ํด๊ฒฐ๋ฒ : ๊ฐ ๋ฌธ์์ด ์์ ๊ธธ์ด๋ฅผ ๋ถ์ฌ์ ์ธ์ฝ๋ฉ ํจ
Example 1. ๋จ๊ณ๋ณ ์ค๋ช
์
๋ ฅ: ["lint","code","love","you"]
์ธ์ฝ๋ฉ ๊ณผ์ :
"lint" โ 4:lint
"code" โ 4:code
"love" โ 4:love
"you" โ 3:you
์ต์ข
์ธ์ฝ๋ฉ ๋ฌธ์์ด: 4:lint4:code4:love3:you
๋์ฝ๋ฉ ๊ณผ์ :
i=0 โ : ์์น 1, ๊ธธ์ด 4 โ ๋ฌธ์์ด lint (i=5)
i=5 โ : ์์น 6, ๊ธธ์ด 4 โ ๋ฌธ์์ด code (i=10)
i=10 โ : ์์น 11, ๊ธธ์ด 4 โ ๋ฌธ์์ด love (i=15)
i=15 โ : ์์น 16, ๊ธธ์ด 3 โ ๋ฌธ์์ด you (i=19)
์ต์ข
๊ฒฐ๊ณผ: ["lint","code","love","you"]
๋์ ์๋ฆฌ ์์ฝ
์ธ์ฝ๋ฉ: ๊ฐ ๋ฌธ์์ด ์์ ๊ธธ์ด๋ฅผ ๋ถ์ฌ์ ํผ๋ ์์ด ๋์ฝ๋ฉ ๊ฐ๋ฅํจ
๋์ฝ๋ฉ: ๊ธธ์ด ์ ๋ณด๋ฅผ ์ด์ฉํด ์ ํํ ๋ฌธ์์ด์ ์ถ์ถํจ
'''
class Solution:
def encode(self, strs):
encoded = []
for s in strs:
encoded.append(f"{len(s)}:{s}") # ๊ฐ ๋ฌธ์์ด ์์ ๊ธธ์ด + ":"์ ๋ถ์
return "".join(encoded) # ๋ชจ๋ ๋ฌธ์์ด์ ํ๋๋ก ํฉ์นจ
def decode(self, s):
decoded = []
i = 0
while i < len(s):
colon = s.find(":", i) # s.find(":", i) : ํ์ฌ ์์น(i)๋ถํฐ ์ฒ์ ๋์ค๋ :์ ์์น๋ฅผ ์ฐพ๊ธฐ
length = int(s[i:colon]) # int(s[i:colon]) : ์์ ์ซ์๋ฅผ ๋ฌธ์์ด ๊ธธ์ด๋ก ๋ณํ
i = colon + 1 # ๋ฌธ์์ด ์์ ์์น๋ก ์ด๋
decoded.append(s[i:i+length]) # s[i:i+length : ๊ธธ์ด๋งํผ ๋ฌธ์์ด์ ์๋ผ์ ์ ์ฅํจ.
i += length # ๋ค์ ๋ฌธ์์ด์ ์์ ์์น๋ก ์ด๋
return decoded