문제풀이 2021 01 13
2021.01.13 연습
- 사용언어 : java
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<Integer>();
for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
for (int i = 1 ; i <= nums.length; i++) {
if (!map.containsKey(i)) {
list.add(i);
}
}
return list;
}
}
Runtime: 22 ms, faster than 10.43% of Java online submissions for Find All Numbers Disappeared in an Array. Memory Usage: 48.8 MB, less than 13.99% of Java online submissions for Find All Numbers Disappeared in an Array.
- 다른 코드 참고
//making numbers index negative if that number is present(Mark them visited/present)
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
for(int i=0;i<nums.length;i++)
{
int j= Math.abs(nums[i]) - 1;
nums[j]=-1*Math.abs(nums[j]); //make negative
}
List<Integer> res = new ArrayList<>();
for(int i=0;i<nums.length;i++)
{
if(nums[i]>0){
res.add(i+1);
}
}
return res;
}
}