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