Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Real or Not? NLP with Disaster Tweets
- 캐치카페
- Baekjoon
- ubuntu
- 프로그래머스 파이썬
- programmers
- 프로그래머스
- 맥북
- 자연어처리
- 편스토랑 우승상품
- dacon
- Docker
- 금융문자분석경진대회
- PYTHON
- Git
- 코로나19
- 편스토랑
- Kaggle
- gs25
- 우분투
- SW Expert Academy
- 데이콘
- 더현대서울 맛집
- leetcode
- github
- AI 경진대회
- 백준
- hackerrank
- 파이썬
- ChatGPT
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 268. Missing Number (Python) 본문
728x90
반응형
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
Solution
class Solution:
def missingNumber(self, nums: List[int]) -> int:
max_num = max(nums)
my_nums = set([num for num in range(max_num + 1)])
nums = set(nums)
differ = my_nums - nums
if len(differ) == 0:
answer = max_num + 1
else:
answer = list(differ)[0]
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 891. Sum of Subsequence Widths (Python) (0) | 2020.06.11 |
---|---|
[HackerRank] HackerRank in a String! (Python) (0) | 2020.06.10 |
[leetCode] 344. Reverse String (Python) (0) | 2020.06.08 |
[leetCode] 326. Power of Three (Python) (0) | 2020.06.07 |
[leetCode] 204. Count Primes (Python) (0) | 2020.06.06 |
Comments