Problems
์ ์ผ ๊ธด common prefix๋ฅผ ์ฐพ๋ ๋ฌธ์ ์ด๋ค.
์ด๊ฑธ ํ๋ํ๋ ์ด๋ป๊ฒ ๊ฒ์ฌํด์ผํ๋ ๊ณ ๋ฏผ์ด ๋์๋ค.
์ฐ์ ์ ์ผ ์งง์ ๋จ์ด๋ฅผ ๊ธฐ์ค์ ์ก๊ณ ์์ํด์ผ๊ฒ ๋ค๊ณ ์๊ฐํ๋ค.
์ฒ์์ shortest_word = min(strs, len) ์ผ๋ก ํ๋ ์ง์ ์ ํ ์ํ๋ก ์์ํ๋๋ฐ,
์ ๊ทธ๋ฅ ์ ๋ ฌ๋ก ์ฒซ๋ฒ์งธ ๋ฐ์๋๊ณ ํ๋ฉด ์ข ๋ ์์ํ๊ฒ ๋ค ์๊ฐํ๋ค
strs.sort(key = lambda x : len(x))
for i in range(len(strs[0])):
for j in range(1, len(strs)):
if strs[0][i] != strs[j][i]:
return strs[0][:i]
return strs[0]
์ ์ผ ์งง์ ๋จ์ด str[0] ์ ๊ธฐ์ค์ ์ก๊ณ , ๊ทธ ๋ค์ ๋จ์ด๋ถํฐ ํ๋ํ๋์ฉ ํ์ธํ๋ค.
๋ง์ฝ ๋ค๋ฅธ ๋ฌธ์๊ฐ ๋์จ๋ค๋ฉด ๊ทธ ์์ prefix๊น์ง๋ง return ํ๊ณ ๋๋ด๋๋ก ํ๋ค.
์ฑ ~ ๊ณต
Review
์ด ๋ฌธ์ ๋ ๋๋ฌด ํท๊ฐ๋ ค์ ํ๋ค๊ฐ ๋ค๋ฅธ ์ฌ๋๋ค์ ํ์ด๋ฅผ ๋ดค๋๋ฐ
๋ณด๊ณ ๋๋๊น ๋ ํท๊ฐ๋ ธ๋ค. startswith, enumerate, zip ,, ๋ฑ๋ฑ ๋ค์ํ ํจ์๋ค์ ์ฌ์ฉํด์ ๊ฐ๋จํ๊ฒ ํ์ด๋ด๋ ์ฌ๋๋ค์ ๋ณด๋ฉด์
๋๋ ๋ง์ด ๊ณต๋ถํด์ผ๊ฒ ๊ตฌ๋ ํ๋ค ..
'๐ป leetcode > algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] Algorithms | 21. Merge Two Sorted Lists | Python (1) | 2024.07.24 |
---|---|
[LeetCode] Algorithms | 20. Valid Parentheses | Python (0) | 2024.07.23 |
[LeetCode] Algorithms | 13. Roman to Integer | Python (0) | 2024.07.12 |