알고리즘

[LeetCode]Find All Numbers Disappeared in an Array

JUNGKEUNG 2021. 9. 6. 17:33

https://leetcode.com/explore/learn/card/fun-with-arrays/523/conclusion/3270/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

Success

배열을 정렬하고 [1,n] 사이에 없는 숫자를 찾아 넣어야한다.

 

풀이

먼저 nums의 배열을 정렬한 후 nums의 길이을 i만큼 돌리는데 binarySearch 메서드를 사용하여 없는 숫자를 넣는다.

 

다른 풀이

  • n개의 수를 저장하는 배열에서, 없는 수를 찾아 출력한다.
  • nums[i]의 값을 index로 가지고 있는 boolean 배열 checkArray를 만든다.
  • 있으면 true, 없으면 false로 저장한다.
  • 다음의 for문에서, checkArray가 faluse이면 nums[i]의 값이 없는 것이므로 해당 i를 result에 추가한다.