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
'Python > PS in Python' 카테고리의 다른 글
LeetCode 169. Majority Element - Python (0) | 2020.08.13 |
---|---|
LeetCode 226. Invert Binary Tree - Python (0) | 2020.08.09 |
LeetCode 104. Maximum Depth of Binary Tree - Python (0) | 2020.08.04 |
LeetCode 617. Merge Two Binary Trees - Python (0) | 2020.08.03 |
[코드업 기초 100제] 1001~1099 (파이썬) - 문제 풀이용 (0) | 2020.07.28 |
댓글