🔍 문제
Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]
Input: nums = [1,1]
Output: [2]
Tip) The intuition behind using a hash map is pretty clear in this case
🖥 실행
1) Answer(Hash Table)
class Solution(object):
def findDisappearedNumbers(self, nums):
hash_table = {} #dict
for num in nums:
hash_table[num] = 1
result = []
for num in range(1, len(nums) + 1):
if num not in hash_table:
result.append(num)
return result
2) Better Solution(Space InPlace Modification Solution)
class Solution(object):
def findDisappearedNumbers(self, nums):
hash_table = {}
for num in nums:
hash_table[num] = 1
result = []
for num in range(1, len(nums) + 1):
if num not in hash_table:
result.append(num)
return result
'Coding test > LeetCode' 카테고리의 다른 글
[LeetCode] Third Maximum Number (0) | 2022.05.20 |
---|---|
[LeetCode] Max Consecutive Ones II (0) | 2022.05.20 |
[LeetCode] Height Checker (0) | 2022.05.20 |
[LeetCode] Move Zeroes (0) | 2022.05.20 |
[LeetCode] Replace Elements with Greatest Element on Right Side (0) | 2022.05.20 |