[YOOHYOJEONG] WEEK 02 solutions#2400
[YOOHYOJEONG] WEEK 02 solutions#2400YOOHYOJEONG merged 2 commits intoDaleStudy:mainDaleStudy/leetcode-study:mainfrom
Conversation
dohyeon2
left a comment
There was a problem hiding this comment.
๋ฐฐ์ด๋ถ๋ถ์ ์ฃผ์์ ์ ๋ ๋ถ๋ถ๋ ์ธ์์ ์
๋๋ค.
์ด๋ค ๊ณ ๋ฏผ์ ๊ฐ์ง๊ณ ์ ๊ทผํ์
จ๋์ง ์ ์ ์์ด์ ํฅ๋ฏธ๋ก์ ์ต๋๋ค.
ํ์ด์ฌ์ด ํ์คํ ๊ฐ๋จํ ์ฝ๋์ ์์ด์๋ ๊ฐ๋
์ฑ์ด ์๋์ ์ด๋ค์.
|
|
||
| a, b = 1, 2 | ||
| for i in range(3, n+1): | ||
| a, b = b, a+b |
There was a problem hiding this comment.
ํ์ด์ฌ์ ๋ค์ค ํ ๋น์ด ํธ๋ฆฌํ๋ค์.
| left *= nums[i] | ||
|
|
||
| right = 1 | ||
| for i in range(n-1, -1, -1): |
There was a problem hiding this comment.
๊ฐ์ธ์ ์ผ๋ก๋ for๋ฌธ ์กฐ๊ฑด์์ ๊ฐ์ฐํ๋ ํํ๋ก ์ฌ์ฉํ๋ ๊ฑธ ์ง์ํ๋ ํธ์ธ๋ฐ,
'๋ช๋ฒ ๋ฐ๋ณตํ๋๋ก ๋์ด์๋๊ฐ?'๋ผ๋ ์ธก๋ฉด์์ ๊ฐ๋
์ฑ์ด ์กฐ๊ธ ๋จ์ด์ง๋ค๊ณ ์๊ฐํ๊ธฐ๋๋ฌธ์
๋๋ค.
ํจ์ ๋์ ์ด๋ป๊ฒ ์๊ฐํ์๋์? ใ
ใ
์๊ฐ์ ๋๋๊ณ ์ถ์ด ์ฝ๋ฉํธ ๋จ๊น๋๋ค.
for i in range(n):
idx = n - i
output[idx] *= right
...There was a problem hiding this comment.
์ข์ ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค!!
์ค๋ฅธ์ชฝ ๋์ ๊ณฑ์ ๊ณ์ฐํ๋ ค๊ณ ์ญ์์ผ๋ก ์ํํ๋ฉด์ ์์ฑํ๋๋ฐ ๋ง์ํด์ฃผ์ ๊ฒ์ฒ๋ผ ๋ฐ๋ณต ํ์ ๊ด์ ์์๋ ๊ฐ๋
์ฑ์ด ๋จ์ด์ง ์ ์๋ค๋ ์ ๊ณต๊ฐํฉ๋๋ค.
Python์์๋ ๋ณดํต reversed(range(n)) ๊ฐ์ ๋ฐฉ์์ผ๋ก๋ ํํํ ์ ์์ ๊ฒ ๊ฐ์์ ๊ทธ ๋ถ๋ถ๋ ํ ๋ฒ ๊ณ ๋ คํด ๋ณผ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค!!
|
|
||
| class Solution(object): | ||
| def isAnagram(self, s, t): | ||
| if sorted(s) == sorted(t): |
There was a problem hiding this comment.
sortedํจ์๊ฐ ๋๋ถ๋ถ์ ์ธ์ด์์ n log n ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ง๊ธฐ ๋๋ฌธ์,
๋ณด๋ค ๋ ํจ์จ์ ์ธ ๋ฐฉ๋ฒ(O(n))์ด ์์ ๊ฒ ๊ฐ์ต๋๋ค!
There was a problem hiding this comment.
@hyeri0903 ๋ ๋๋ถ์ Counter๋ฅผ ์ฌ์ฉํ๋ฉด ์ ๋ ฌ ์์ด O(n) ์๊ฐ๋ณต์ก๋๋ก ํด๊ฒฐํ ์ ์๋ค๋ ์ ์ ์๊ฒ ๋์์ต๋๋ค!!
๋ ๋ถ ๋ชจ๋ ๊ฐ์ฌํฉ๋๋ค!
|
|
||
| a, b = 1, 2 | ||
| for i in range(3, n+1): | ||
| a, b = b, a+b |
There was a problem hiding this comment.
๋ค๋ฅธ ๋ณ์๋ฅผ ํ ๋นํ์ง ์๊ณ ์ด๋ ๊ฒ๋ ํ์ดํ ์ ์๊ตฐ์ ใ ใ ๐
| def isAnagram(self, s, t): | ||
| if sorted(s) == sorted(t): | ||
| return True | ||
| else: |
There was a problem hiding this comment.
if ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ง ์๊ณ ํ์ค๋ก ์์ฑํ ์๋ ์์๊ฒ ๊ฐ๋ค์!
return sorted(s) == sorted(t)
Counter ๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์๊ฐ ๋ณต์ก๋๋ฅผ O(n)์ผ๋ก ์ค์ผ ์ ์์๊ฒ ๊ฐ์ต๋๋ค.
from collections import Counter
def isAnagram(self, s, t):
return Counter(s) == Counter(t)
There was a problem hiding this comment.
์ ๋ ฌ์ ์ด์ฉํ ๋ฐฉ์์ด ๊ฐ๋จํ ๊ฒ ๊ฐ์์ ์ด๋ ๊ฒ ํ์๋๋ฐ ๋ง์ํด์ฃผ์ ๊ฒ์ฒ๋ผ Counter๋ฅผ ์ฌ์ฉํ๋ฉด O(n)์ผ๋ก ๋ ํจ์จ์ ์ผ๋ก ํด๊ฒฐํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค!
๋ return sorted(s) == sorted(t)์ฒ๋ผ ๋ ๊ฐ๊ฒฐํ๊ฒ ํํํ ์๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค!
์ข์ ๋ฐฉ๋ฒ ๊ณต์ ํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค!
|
์๊ณ ๋ฆฌ์ฆ์ ๊น๋ํ๊ฒ ์ ํธ์์ง๋ง ์๋ฃจ์ ์๋จ์ ์๊ฐ ๋ณต์ก๋, ๊ณต๊ฐ ๋ณต์ก๋ ๊ทธ๋ฆฌ๊ณ ๊ฐ๋จํ ํ์ด ๋ฐฉ๋ฒ์ ์ฃผ์์ผ๋ก ์์ฑํ๊ณ ํ์ดํ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค! ๋ค์์ฃผ๋ ํ์ดํ ํด์๐ |
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!