Published:
Updated:

  • ์ฒ˜์Œ์—๋Š” sorted(num)์ด ๊ผญ ํ•„์š”ํ•œ ์ค„ ์•Œ์•˜๋Š”๋ฐ ์ •๋ ฌ์„ ์•ˆ ํ•ด์ค˜๋„ ๊ฒฐ๊ตญ ๋จ
    • ํ—ท๊ฐˆ๋ฆฌ๋ฉด ์ง์ ‘ ๋ฐฐ์—ด example์„ ์ƒ๊ฐํ•ด ๋ณด์…ˆ


SolutionPermalink

from typing import List


class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        answer = 0

        for num in sorted(nums):
            # answer์— num ๊ฐ’์„ ๊ณ„์† ์ €์žฅํ•˜๋Š”๋ฐ, "^"์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต๋œ ๊ฐ’์ด ์ €์žฅ๋˜์ง€ ์•Š๊ฒŒ ํ•จ
            # ๋งŒ์•ฝ [2(1), 2(2), 1]์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ,
            # 2(1) ์ด๋ฏธ ์ €์žฅ๋˜์–ด ์žˆ์œผ๋ฉด 2(1)๊ณผ 2(1)์˜ "^" ์—ฐ์‚ฐ์€ 0์ด ๋จ
            # ์ฆ‰, ์ตœ์ข…์ ์œผ๋กœ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๊ฐ’์ด ์ €์žฅ๋˜๋Š” ๊ฑฐ์ง€
            answer = answer ^ num

        return answer


ReferencePermalink

Leave a comment