| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- GitLab
- Code Jam 2022
- 문제해결
- 프로그래머스
- First Unique Character in a String
- 하늘과 바람과 별과 詩
- 파이썬
- Algorithm
- Qualification Round
- 2022
- Kubernetes
- C++
- Count Monobit Integers
- 리트코드
- hackerrank
- LEVEL 2
- 3D PRINTING
- MySQL
- 알고리즘
- Code Jam
- Python
- ProblemSolving
- 해커랭크
- swift
- 코딩테스트
- leetcode
- Certbot/dns-route53
- 하늘과 바람과 별과 시
- K8S
Archives
- Today
- Total
공대생의 비망록
[LeetCode][Easy] Single Number 문제 Python 풀이 본문
Programming Language/Python
[LeetCode][Easy] Single Number 문제 Python 풀이
myungsup1250 2026. 2. 8. 15:38set으로 만들어서 고유한 숫자만 남겨 sum을 2배로 하여 nums의 sum 값과 차감하여 숫자를 찾아내는 풀이:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
mySum = sum(set(nums)) * 2
diff = mySum - sum(nums)
return diff
XOR 비트 연산으로 배열을 순회하여 숫자를 찾아내는 풀이: (연달아 XOR 비트 연산으로 풀 수 있는 문제를 만나 습득하게 되었습니다.)
class Solution:
def singleNumber(self, nums: List[int]) -> int:
res = 0
for num in nums:
res ^= num
return res'Programming Language > Python' 카테고리의 다른 글
| [LeetCode][Easy] Valid Anagram 문제 Python 풀이 (0) | 2026.02.08 |
|---|---|
| [LeetCode][Easy] Count Monobit Integers 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Missing Number 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Move Zeroes 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Merge Sorted Array 문제 Python 풀이 (0) | 2026.02.07 |
Comments
