forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJay-Mo-99.py
More file actions
42 lines (33 loc) ยท 1.82 KB
/
Jay-Mo-99.py
File metadata and controls
42 lines (33 loc) ยท 1.82 KB
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
32
33
34
35
36
37
38
39
40
๏ปฟ #ํด์
#nums๊ฐ 0์์ len(nums) ๊น์ง์ ์ซ์๋ฅผ ํฌํจํ๋ ํ์ธํ๊ณ , ์๋ค๋ฉด ํด๋น ์ซ์๋ฅผ returnํ๋ค.
#nums๋ฅผ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๋ค
#nums๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด, nums[i]๋ ์ธ๋ฑ์ค i์ ๋์ผํด์ผํ๋ค. (e.g nums[0] =0, nums[1]=1)
#๋ฐฐ์ด์ ๋ง์ง๋ง ์์(nums(len(num-1))) ์ด len(nums)์ ๋์ผํ์ง ์์ผ๋ฉด nums์ 0~len(nums) ๊น์ง์ ์ซ์๋ฅผ ๊ฐ์ง๋ค๋ ์กฐ๊ฑด์ ๋ง์กฑ X -> ๋๋ฝ๋ len(nums)๋ฅผ returnํ๋ค.
#for loop ๋ก ๊ฐ ์ซ์๊ฐ ์ธ๋ฑ์ค์ ์ผ์น ์ฌ๋ถ ํ์ธ
#์ธ๋ฑ์ค์ ๊ฐ์ด ์ผ์นํ์ง ์๋ nums[i]์ ์ธ๋ฑ์ค๋ฅผ returnํ๋ค.
#Big O
#N: ๋งค๊ฐ๋ณ์ n์ ํฌ๊ธฐ(๊ณ๋จ ๊ฐฏ์)
#Time Complexity: O(nlog(n)) = O(nlog(n))+O(1)+O(n)
#- n: nums๋ฐฐ์ด์ ๊ธธ์ด
#- sort(): Timsort๋ฐฉ์์ด๊ธฐ์ O(nlog(n))
#-if(nums[len(nums)-1] != len(nums)): ๋จ์ผ ์กฐ๊ฑด ๋น๊ต๋ O(1)
#for loop: nums์ ๊ธธ์ด์ ๋น๋กํ๋ฏ๋ก O(n)
#Space Complexity: O(1)
#- sort(): nums.sort()๋ ์ ์๋ฆฌ ์ ๋ ฌ์ด๊ธฐ์ ์ถ๊ฐ ๊ณต๊ฐ ํ์์น ์์ผ๋ฏ๋ก O(1)
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Sort the list
nums.sort()
# If the last element of nums doesn't align with the numbers of element in nums, return len(nums)
# For example, nums[0,1] so range is [0:2] but there's no last element of 2 so return 2(len(nums))
if(nums[len(nums)-1] != len(nums)):
return len(nums)
#If each element doesn't match with the number of [0:len(nums)], return the i(index)
for i in range(len(nums)):
if nums[i] != i:
print(nums[i],i)
return i