Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

448. 找到所有数组中消失的数字 #90

Open
webVueBlog opened this issue Sep 6, 2022 · 0 comments
Open

448. 找到所有数组中消失的数字 #90

webVueBlog opened this issue Sep 6, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

448. 找到所有数组中消失的数字

Description

Difficulty: 简单

Related Topics: 数组, 哈希表

给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums 中的数字,并以数组的形式返回结果。

示例 1:

输入:nums = [4,3,2,7,8,2,3,1]
输出:[5,6]

示例 2:

输入:nums = [1,1]
输出:[2]

提示:

  • n == nums.length
  • 1 <= n <= 105
  • 1 <= nums[i] <= n

**进阶:**你能在不使用额外空间且时间复杂度为O(n)的情况下解决这个问题吗? 你可以假定返回的数组不算在额外空间内。

Solution

Language: JavaScript

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var findDisappearedNumbers = function(nums) {
 const map = new Map(), res = [];
 for (const num of nums) {
  map.set(num, true)
 }
 for (let i = 1; i <= nums.length; i++) {
  if (!map.get(i)) {
   res.push(i)
  }
 }
 return res
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant