Skip to content

Commit bb157b1

Browse files
Create taurus09318976.py
1 parent 5399839 commit bb157b1

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
๋ชฉํ‘œ : ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์ธ์ฝ”๋”ฉํ•˜๊ณ , ๋‹ค์‹œ ์›๋ž˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋””์ฝ”๋”ฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ๋“ฌ์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ์ž„
3+
ํ•ต์‹ฌ : ๋ฌธ์ž์—ด์— ์–ด๋–ค ๋ฌธ์ž๊ฐ€ ๋“ค์–ด ์žˆ๋”๋ผ๋„ ์ •ํ™•ํžˆ ์ธ์ฝ”๋”ฉ/๋””์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•จ
4+
ํ•ด๊ฒฐ๋ฒ• : ๊ฐ ๋ฌธ์ž์—ด ์•ž์— ๊ธธ์ด๋ฅผ ๋ถ™์—ฌ์„œ ์ธ์ฝ”๋”ฉ ํ•จ
5+
6+
Example 1. ๋‹จ๊ณ„๋ณ„ ์„ค๋ช…
7+
์ž…๋ ฅ: ["lint","code","love","you"]
8+
9+
์ธ์ฝ”๋”ฉ ๊ณผ์ •:
10+
"lint" โ†’ 4:lint
11+
"code" โ†’ 4:code
12+
"love" โ†’ 4:love
13+
"you" โ†’ 3:you
14+
15+
์ตœ์ข… ์ธ์ฝ”๋”ฉ ๋ฌธ์ž์—ด: 4:lint4:code4:love3:you
16+
17+
๋””์ฝ”๋”ฉ ๊ณผ์ •:
18+
i=0 โ†’ : ์œ„์น˜ 1, ๊ธธ์ด 4 โ†’ ๋ฌธ์ž์—ด lint (i=5)
19+
i=5 โ†’ : ์œ„์น˜ 6, ๊ธธ์ด 4 โ†’ ๋ฌธ์ž์—ด code (i=10)
20+
i=10 โ†’ : ์œ„์น˜ 11, ๊ธธ์ด 4 โ†’ ๋ฌธ์ž์—ด love (i=15)
21+
i=15 โ†’ : ์œ„์น˜ 16, ๊ธธ์ด 3 โ†’ ๋ฌธ์ž์—ด you (i=19)
22+
23+
์ตœ์ข… ๊ฒฐ๊ณผ: ["lint","code","love","you"]
24+
25+
๋™์ž‘ ์›๋ฆฌ ์š”์•ฝ
26+
์ธ์ฝ”๋”ฉ: ๊ฐ ๋ฌธ์ž์—ด ์•ž์— ๊ธธ์ด๋ฅผ ๋ถ™์—ฌ์„œ ํ˜ผ๋™ ์—†์ด ๋””์ฝ”๋”ฉ ๊ฐ€๋Šฅํ•จ
27+
๋””์ฝ”๋”ฉ: ๊ธธ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ์ •ํ™•ํžˆ ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•จ
28+
'''
29+
30+
31+
class Solution:
32+
def encode(self, strs):
33+
encoded = []
34+
for s in strs:
35+
encoded.append(f"{len(s)}:{s}") # ๊ฐ ๋ฌธ์ž์—ด ์•ž์— ๊ธธ์ด + ":"์„ ๋ถ™์ž„
36+
return "".join(encoded) # ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜๋กœ ํ•ฉ์นจ
37+
38+
def decode(self, s):
39+
decoded = []
40+
i = 0
41+
while i < len(s):
42+
colon = s.find(":", i) # s.find(":", i) : ํ˜„์žฌ ์œ„์น˜(i)๋ถ€ํ„ฐ ์ฒ˜์Œ ๋‚˜์˜ค๋Š” :์˜ ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ
43+
length = int(s[i:colon]) # int(s[i:colon]) : ์•ž์˜ ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด ๊ธธ์ด๋กœ ๋ณ€ํ™˜
44+
i = colon + 1 # ๋ฌธ์ž์—ด ์‹œ์ž‘ ์œ„์น˜๋กœ ์ด๋™
45+
decoded.append(s[i:i+length]) # s[i:i+length : ๊ธธ์ด๋งŒํผ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ์„œ ์ €์žฅํ•จ.
46+
i += length # ๋‹ค์Œ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์œ„์น˜๋กœ ์ด๋™
47+
return decoded

0 commit comments

Comments
ย (0)