Python/PS in Python
LeetCode 136. Single Number - Python
Air’s Big Data
2020. 8. 6. 18:22
LeetCode 136. Single Number - Python
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
Solution 1 :
class Solution:
def singleNumber(self, nums: List[int]) -> int:
cnt_dict = Counter(nums)
items = cnt_dict.items()
for key, val in items:
if val == 1:
answer = key
break
return answer
Solution 2 :
class Solution:
def singleNumber(self, nums: List[int]) -> int:
answer = 0
for i in nums:
answer = answer ^ i
return answer
내가 틀린 코드
n2 = map(int, input().split())
n2_list = list(n2)
for i in range(n2):
if i != int(n2):
continue
print(i, end='')
참고 개념 :
countercollections 모듈 - Counter
dictionary 의 key, value 를 출력 하는 방법.
딕셔너리 자료형 : wikidocs.net/16