from typing import List
class Solution:
# ๋น์ด ์์ง ์์ ์ ์ ๋ฐฐ์ด์ด ์ฃผ์ด์ก์ ๋, ํ ๊ฐ๋ฅผ ์ ์ธํ ๋ชจ๋ ์์๊ฐ ๋ ๋ฒ ๋ํ๋จ
# ๊ทธ ์ ์ธ๋ ํ ๊ฐ์ ์์๊ฐ ๋ฌด์์ธ์ง ์ฐพ์!
# linear runtime complexity: O(N) ์๊ฐ ๋ณต์ก๋
def singleNumber(self, nums: List[int]) -> int:
# ์ฐ๋ ๊ธฐ
# nums.sort()
# # ์ ๋ ฌํ๋ฉด ๋ต์ nums[len(nums) - 1] or nums[0]
# if len(nums) > 1:
# if nums[0] == nums[1]:
# return nums[len(nums) - 1]
# if nums[len(nums) - 1] == nums[len(nums) - 2]:
# return nums[0]
# else: # Example 3๊ณผ ๊ฐ์ ์ํฉ
# return nums[0]
# i-1๊ณผ i๋ฅผ ๊ณ์ ๋๋ฉด์ cnt๋ฅผ ์ฆ๊ฐ์์ผ๋ณผ๊น..? -> X
# ์์ฒ๋ผ ์ฐ์ง ๋ง๊ณ XOR ๋นํธ ์ฐ์ฐ์ ์ฌ์ฉํ์
# 2๊ฐ๊ฐ ๊ฐ์ ๋ ์ฒดํฌ๋๋ ์์ด๋์ด๋ก!
answer = 0
for i in nums:
answer ^= i
return answer
print(Solution().singleNumber([2, 2, 1]))
print(Solution().singleNumber([4, 1, 2, 1, 2]))
print(Solution().singleNumber([1]))
# Expected: 354
print(Solution().singleNumber(
[-336, 513, -560, -481, -174, 101, -997, 40, -527, -784, -283, -336, 513, -560, -481, -174, 101, -997, 40, -527,
-784, -283, 354]))
Leave a comment