deda
Deda의 데이터 디자인
deda
전체 방문자
오늘
어제
  • 분류 전체보기 (121)
    • Python (27)
      • Python 기초 (17)
      • Python 데이터분석 (10)
    • SQL (9)
    • Coding test (54)
      • Python 기초문제 (45)
      • LeetCode (9)
    • BigData (2)
    • ZeroBase (3)
    • UX (0)
    • Business Review (1)
    • 통계 & 수학 (17)
      • 통계학 (14)
      • 수학 (3)
    • 스터디 (6)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 통계
  • 기초수학
  • matplotlib
  • 최소공배수
  • 파이썬
  • pandas
  • 모듈
  • 네카라쿠배
  • 소인수분해
  • 코딩
  • 부트캠프
  • 마이데이터
  • 데이터사이언티스트
  • 제로베이스
  • SQL
  • 군수열
  • 데이터분석가
  • 데이터엔지니어
  • 등비수열
  • BMI
  • 데이터분석
  • 함수
  • 프로그래밍
  • 릿코드
  • 계산기
  • 빅데이터
  • 팩토리얼
  • 미니콘다
  • 계차수열
  • 등차수열

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
deda

Deda의 데이터 디자인

[LeetCode] Find All Numbers Disappeared in an Array
Coding test/LeetCode

[LeetCode] Find All Numbers Disappeared in an Array

2022. 5. 23. 14:53

🔍  문제

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
    'Coding test/LeetCode' 카테고리의 다른 글
    • [LeetCode] Third Maximum Number
    • [LeetCode] Max Consecutive Ones II
    • [LeetCode] Height Checker
    • [LeetCode] Move Zeroes
    deda
    deda
    데이터 분석 / 파이썬 / UX / 정량리서치

    티스토리툴바