|
| 1 | +๏ปฟ #ํด์ |
| 2 | + #nums๊ฐ 0์์ len(nums) ๊น์ง์ ์ซ์๋ฅผ ํฌํจํ๋ ํ์ธํ๊ณ , ์๋ค๋ฉด ํด๋น ์ซ์๋ฅผ returnํ๋ค. |
| 3 | + #nums๋ฅผ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๋ค |
| 4 | + #nums๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด, nums[i]๋ ์ธ๋ฑ์ค i์ ๋์ผํด์ผํ๋ค. (e.g nums[0] =0, nums[1]=1) |
| 5 | + #๋ฐฐ์ด์ ๋ง์ง๋ง ์์(nums(len(num-1))) ์ด len(nums)์ ๋์ผํ์ง ์์ผ๋ฉด nums์ 0~len(nums) ๊น์ง์ ์ซ์๋ฅผ ๊ฐ์ง๋ค๋ ์กฐ๊ฑด์ ๋ง์กฑ X -> ๋๋ฝ๋ len(nums)๋ฅผ returnํ๋ค. |
| 6 | + #for loop ๋ก ๊ฐ ์ซ์๊ฐ ์ธ๋ฑ์ค์ ์ผ์น ์ฌ๋ถ ํ์ธ |
| 7 | + #์ธ๋ฑ์ค์ ๊ฐ์ด ์ผ์นํ์ง ์๋ nums[i]์ ์ธ๋ฑ์ค๋ฅผ returnํ๋ค. |
| 8 | + |
| 9 | + |
| 10 | + #Big O |
| 11 | + #N: ๋งค๊ฐ๋ณ์ n์ ํฌ๊ธฐ(๊ณ๋จ ๊ฐฏ์) |
| 12 | + |
| 13 | + #Time Complexity: O(nlog(n)) = O(nlog(n))+O(1)+O(n) |
| 14 | + #- n: nums๋ฐฐ์ด์ ๊ธธ์ด |
| 15 | + #- sort(): Timsort๋ฐฉ์์ด๊ธฐ์ O(nlog(n)) |
| 16 | + #-if(nums[len(nums)-1] != len(nums)): ๋จ์ผ ์กฐ๊ฑด ๋น๊ต๋ O(1) |
| 17 | + #for loop: nums์ ๊ธธ์ด์ ๋น๋กํ๋ฏ๋ก O(n) |
| 18 | + |
| 19 | + |
| 20 | + #Space Complexity: O(1) |
| 21 | + #- sort(): nums.sort()๋ ์ ์๋ฆฌ ์ ๋ ฌ์ด๊ธฐ์ ์ถ๊ฐ ๊ณต๊ฐ ํ์์น ์์ผ๋ฏ๋ก O(1) |
| 22 | + |
| 23 | +class Solution(object): |
| 24 | + def missingNumber(self, nums): |
| 25 | + """ |
| 26 | + :type nums: List[int] |
| 27 | + :rtype: int |
| 28 | + """ |
| 29 | + # Sort the list |
| 30 | + nums.sort() |
| 31 | + # If the last element of nums doesn't align with the numbers of element in nums, return len(nums) |
| 32 | + # For example, nums[0,1] so range is [0:2] but there's no last element of 2 so return 2(len(nums)) |
| 33 | + if(nums[len(nums)-1] != len(nums)): |
| 34 | + return len(nums) |
| 35 | + #If each element doesn't match with the number of [0:len(nums)], return the i(index) |
| 36 | + for i in range(len(nums)): |
| 37 | + if nums[i] != i: |
| 38 | + print(nums[i],i) |
| 39 | + return i |
| 40 | + |
| 41 | + |
| 42 | + |
0 commit comments