[Week 6-3] ๐Ÿ”ฅํŠน๊ฐ• 3์ผ์ฐจ

6์ฃผ์ฐจ ๋ชฉ์š”์ผ

  • ๋ฆฟ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€

[LeetCode 268. Missing Number]

0~n๊นŒ์ง€์˜ ์ˆ˜ ์ค‘ n๊ฐœ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ๋น ์ง„ ์ˆ˜ ์ฐพ๊ธฐ

  • ์ ‘๊ทผ
    ์ด๊ฑฐ์•ผ๋ง๋กœ set์„ ์จ๋จน์„ ๋ฌธ์ œ๋‹ค! ๋ฒ”์œ„๋ฅผ ์•Œ๊ณ  ์žˆ์œผ๋‹ˆ๊นŒ set(1~n)๊ณผ nums์˜ ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ•˜๋ฉด ๋~
  • ์ฝ”๋“œ
class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        # 0~n ๋ฒ”์œ„์ธ๋ฐ ์ˆ˜๋Š” n๊ฐœ๋งŒ ๋“ค์–ด์žˆ์Œ. ๋น ์ง„ ์ˆ˜ ์ฐพ๊ธฐ
        n = len(nums)
        [res] = set(range(n+1)) - set(nums)
        
        return res


์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ