ID | Title | Difficulty | |
---|---|---|---|
Loading... |
448. Find All Numbers Disappeared in an Array
Easy
LeetCode
Array, Hash Table
Problem
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.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]
Example 2:
Input: nums = [1,1]
Output: [2]
Code
参考 442
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
for(int i = 0; i < nums.length; i++) {
int index = Math.abs(nums[i]) - 1;
if(nums[index] > 0) {
nums[index] = -nums[index];
}
}
List<Integer> res = new ArrayList<>();
for(int i = 0; i < nums.length; i++) {
if(nums[i] > 0) {
res.add(i + 1);
}
}
return res;
}
}
按 <- 键看上一题!
447. Number of Boomerangs
按 -> 键看下一题!
449. Serialize and Deserialize BST