forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyyyyyyyyyKim.py
More file actions
37 lines (30 loc) ยท 1.13 KB
/
yyyyyyyyyKim.py
File metadata and controls
37 lines (30 loc) ยท 1.13 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
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
# write your code here
answer = ""
# ๋ฌธ์์ด ์์ "๋ฌธ์์ด๊ธธ์ด + :" ๋ถ์ฌ์ ์ธ์ฝ๋ฉ
for i in strs:
answer += str(len(i)) + ":" + i
return answer
"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, s): # ๋ณ์๋ช
str์ s๋ก ์์
# write your code here
answer = []
i = 0
# ":"๋ฅผ ๊ธฐ์ค์ผ๋ก ์์ ์ซ์(๋ฌธ์์ด๊ธธ์ด)๋งํผ ๋์ด์ ๋์ฝ๋ฉ
while i < len(s):
j = i
# ๋ฌธ์์ด๊ธธ์ด๊ฐ ํ์๋ฆฌ ์ด์์ผ ์ ์์ผ๋๊น ":"๋ฅผ ๊ธฐ์ค์ผ๋ก ํจ
while s[j] != ":":
j += 1
length = int(s[i:j]) # ๋ฌธ์์ด์ ๊ธธ์ด ์ถ์ถ
answer.append(s[j+1:j+1+length]) # ๊ธธ์ด๋งํผ ๋ฌธ์์ด ์ถ์ถํด์ answer์ ๋ฃ๊ธฐ
i = j + 1 + length # ์๋ผ์ง ๋ฌธ์์ด ๋ค๋ก ์ด๋
return answer