返回介绍

solution / 0400-0499 / 0448.Find All Numbers Disappeared in an Array / README_EN

发布于 2024-06-17 01:04:00 字数 5197 浏览 0 评论 0 收藏 0

448. Find All Numbers Disappeared in an Array

中文文档

Description

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]

 

Constraints:

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

 

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Solutions

Solution 1

class Solution:
  def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
    s = set(nums)
    return [x for x in range(1, len(nums) + 1) if x not in s]
class Solution {
  public List<Integer> findDisappearedNumbers(int[] nums) {
    int n = nums.length;
    boolean[] s = new boolean[n + 1];
    for (int x : nums) {
      s[x] = true;
    }
    List<Integer> ans = new ArrayList<>();
    for (int i = 1; i <= n; i++) {
      if (!s[i]) {
        ans.add(i);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> findDisappearedNumbers(vector<int>& nums) {
    int n = nums.size();
    bool s[n + 1];
    memset(s, false, sizeof(s));
    for (int& x : nums) {
      s[x] = true;
    }
    vector<int> ans;
    for (int i = 1; i <= n; ++i) {
      if (!s[i]) {
        ans.push_back(i);
      }
    }
    return ans;
  }
};
func findDisappearedNumbers(nums []int) (ans []int) {
  n := len(nums)
  s := make([]bool, n+1)
  for _, x := range nums {
    s[x] = true
  }
  for i := 1; i <= n; i++ {
    if !s[i] {
      ans = append(ans, i)
    }
  }
  return
}
function findDisappearedNumbers(nums: number[]): number[] {
  const n = nums.length;
  const s: boolean[] = new Array(n + 1).fill(false);
  for (const x of nums) {
    s[x] = true;
  }
  const ans: number[] = [];
  for (let i = 1; i <= n; ++i) {
    if (!s[i]) {
      ans.push(i);
    }
  }
  return ans;
}

Solution 2

class Solution:
  def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
    for x in nums:
      i = abs(x) - 1
      if nums[i] > 0:
        nums[i] *= -1
    return [i + 1 for i in range(len(nums)) if nums[i] > 0]
class Solution {
  public List<Integer> findDisappearedNumbers(int[] nums) {
    int n = nums.length;
    for (int x : nums) {
      int i = Math.abs(x) - 1;
      if (nums[i] > 0) {
        nums[i] *= -1;
      }
    }
    List<Integer> ans = new ArrayList<>();
    for (int i = 0; i < n; i++) {
      if (nums[i] > 0) {
        ans.add(i + 1);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> findDisappearedNumbers(vector<int>& nums) {
    int n = nums.size();
    for (int& x : nums) {
      int i = abs(x) - 1;
      if (nums[i] > 0) {
        nums[i] = -nums[i];
      }
    }
    vector<int> ans;
    for (int i = 0; i < n; ++i) {
      if (nums[i] > 0) {
        ans.push_back(i + 1);
      }
    }
    return ans;
  }
};
func findDisappearedNumbers(nums []int) (ans []int) {
  n := len(nums)
  for _, x := range nums {
    i := abs(x) - 1
    if nums[i] > 0 {
      nums[i] = -nums[i]
    }
  }
  for i := 0; i < n; i++ {
    if nums[i] > 0 {
      ans = append(ans, i+1)
    }
  }
  return
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}
function findDisappearedNumbers(nums: number[]): number[] {
  const n = nums.length;
  for (const x of nums) {
    const i = Math.abs(x) - 1;
    if (nums[i] > 0) {
      nums[i] *= -1;
    }
  }
  const ans: number[] = [];
  for (let i = 0; i < n; ++i) {
    if (nums[i] > 0) {
      ans.push(i + 1);
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文