일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- concurrency
- LeetCode
- 315. Count of Smaller Numbers After Self
- Substring with Concatenation of All Words
- Regular Expression
- 프로그래머스
- DWG
- attribute
- Python Implementation
- Generator
- iterator
- 운영체제
- kaggle
- 715. Range Module
- 컴퓨터의 구조
- Protocol
- 시바견
- Python
- 30. Substring with Concatenation of All Words
- Class
- Convert Sorted List to Binary Search Tree
- t1
- Python Code
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- data science
- 밴픽
- Decorator
- 파이썬
- shiba
Archives
- Today
- Total
Scribbling
LeetCode: 268. Missing Number 본문
The first solution is using binary search.
class Solution:
def missingNumber(self, nums: List[int]) -> int:
left, right = 0, len(nums)
while left < right:
mid = (left + right) // 2
cnt = 0
for num in nums:
if num <= mid:
cnt += 1
if cnt == mid + 1:
left = mid + 1
else:
right = mid
return left
The second solution is using summation.
class Solution:
def missingNumber(self, nums: List[int]) -> int:
ret = len(nums) * (len(nums) + 1) // 2
for num in nums:
ret -= num
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 21. Merge Two Sorted Lists (0) | 2022.01.05 |
---|---|
LeetCode: 442. Find All Duplicates in an Array (0) | 2022.01.05 |
LeetCode: 1010. Pairs of Songs With Total Durations Divisible by 60 (0) | 2022.01.03 |
LeetCode: 297. Serialize and Deserialize Binary Tree (0) | 2022.01.02 |
LeetCode: 328. Odd Even Linked List (0) | 2021.12.31 |