Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 29 | 30 |
| 31 |
Tags
- Mesh
- DS
- CNN
- Python
- 데이터베이스
- sort
- LeetCode
- GNN
- Leet Code
- Database
- SQL
- 오블완
- code
- coding
- db
- 컴퓨터공학
- 데이터
- CS
- 컴퓨터사이언스
- mysql
- 데베
- 자료구조
- 개발자
- 코테
- 코딩테스트
- 티스토리챌린지
- Data_Structure
- 대학생
- adaptive remeshing
- meshgraphnet
Archives
- Today
- Total
sy1214ei 님의 블로그
[Leet Code] 645. Set Mismatch - Python 본문
https://leetcode.com/problems/set-mismatch/description/
Level : Easy
Topic : Array | Hash Table | Bit Manipulation | Sorting
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
# 1~n 까지의 합 : (n(n+1))/2
# nums = [1,2,2,4]
# n = len(nums)
# orgin_sum = (n(n+1))/2 : [1,2,3,4]의 합
# duplicate = nums_sum - set_sum : [1,2,2,4] - [1,2, 4] = 2
# missing = orgin_sum - set_sum : [1,2,3,4] - [1,2, 4] = 3
n = len(nums)
orgin_sum = (n*(n+1)) // 2 # 1~n 까지 합의 공식
duplicate = sum(nums) - sum(set(nums))
missing = orgin_sum - sum(set(nums))
return [duplicate, missing]
# Time Complexity : O(1)
# Space Complexity : O(1)

'[Coding]' 카테고리의 다른 글
| [Leet Code] 9. Palindrome Number - Python (0) | 2024.11.30 |
|---|---|
| [Leet Code] 21. Merge Two Sorted Lists - Python (7) | 2024.11.29 |
| [Leet Code] 58. Length of Last Word - Python (4) | 2024.11.26 |
| [Leet Code] 747. Largest Number At Least Twice of Others - Python (0) | 2024.11.25 |
| [Leet Code] 628. Maximum Product of Three Numbers - Python (0) | 2024.11.25 |