返回介绍

lcci / 17.04.Missing Number / README_EN

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

17.04. Missing Number

中文文档

Description

An array contains all the integers from 0 to n, except for one number which is missing.  Write code to find the missing integer. Can you do it in O(n) time?

Note: This problem is slightly different from the original one the book.

Example 1:


Input: [3,0,1]

Output: 2

 

Example 2:


Input: [9,6,4,2,3,5,7,0,1]

Output: 8

Solutions

Solution 1

class Solution:
  def missingNumber(self, nums: List[int]) -> int:
    nums.sort()
    for i, x in enumerate(nums):
      if i != x:
        return i
    return len(nums)
class Solution {
  public int missingNumber(int[] nums) {
    Arrays.sort(nums);
    int n = nums.length;
    for (int i = 0; i < n; ++i) {
      if (i != nums[i]) {
        return i;
      }
    }
    return n;
  }
}
class Solution {
public:
  int missingNumber(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    int n = nums.size();
    for (int i = 0; i < n; ++i) {
      if (i != nums[i]) {
        return i;
      }
    }
    return n;
  }
};
func missingNumber(nums []int) int {
  sort.Ints(nums)
  for i, x := range nums {
    if i != x {
      return i
    }
  }
  return len(nums)
}
impl Solution {
  pub fn missing_number(mut nums: Vec<i32>) -> i32 {
    nums.sort();
    let n = nums.len() as i32;
    for i in 0..n {
      if i != nums[i as usize] {
        return i;
      }
    }
    n
  }
}
/**
 * @param {number[]} nums
 * @return {number}
 */
var missingNumber = function (nums) {
  nums.sort((a, b) => a - b);
  const n = nums.length;
  for (let i = 0; i < n; ++i) {
    if (i != nums[i]) {
      return i;
    }
  }
  return n;
};

Solution 2

class Solution:
  def missingNumber(self, nums: List[int]) -> int:
    return sum(range(len(nums) + 1)) - sum(nums)
class Solution {
  public int missingNumber(int[] nums) {
    int n = nums.length;
    int ans = n;
    for (int i = 0; i < n; ++i) {
      ans += i - nums[i];
    }
    return ans;
  }
}
class Solution {
public:
  int missingNumber(vector<int>& nums) {
    int n = nums.size();
    int ans = n;
    for (int i = 0; i < n; ++i) {
      ans += i - nums[i];
    }
    return ans;
  }
};
func missingNumber(nums []int) (ans int) {
  ans = len(nums)
  for i, x := range nums {
    ans += i - x
  }
  return
}
impl Solution {
  pub fn missing_number(nums: Vec<i32>) -> i32 {
    let n = nums.len() as i32;
    let mut sum = 0;
    let mut max = 0;
    for num in nums {
      sum += num;
      max = max.max(num);
    }
    if max == n {
      ((1 + max) * max) / 2 - sum
    } else {
      n
    }
  }
}
/**
 * @param {number[]} nums
 * @return {number}
 */
var missingNumber = function (nums) {
  const n = nums.length;
  let ans = n;
  for (let i = 0; i < n; ++i) {
    ans += i - nums[i];
  }
  return ans;
};

Solution 3

class Solution:
  def missingNumber(self, nums: List[int]) -> int:
    ans = 0
    for i, x in enumerate(nums, 1):
      ans ^= i ^ x
    return ans
class Solution {
  public int missingNumber(int[] nums) {
    int ans = 0;
    for (int i = 1; i <= nums.length; ++i) {
      ans ^= i ^ nums[i - 1];
    }
    return ans;
  }
}
class Solution {
public:
  int missingNumber(vector<int>& nums) {
    int ans = 0;
    for (int i = 1; i <= nums.size(); ++i) {
      ans ^= i ^ nums[i - 1];
    }
    return ans;
  }
};
func missingNumber(nums []int) (ans int) {
  for i, x := range nums {
    ans ^= (i + 1) ^ x
  }
  return
}
impl Solution {
  pub fn missing_number(nums: Vec<i32>) -> i32 {
    let mut res = 0;
    let n = nums.len();
    for i in 0..n {
      res ^= nums[i] ^ ((i + 1) as i32);
    }
    res
  }
}
/**
 * @param {number[]} nums
 * @return {number}
 */
var missingNumber = function (nums) {
  let ans = 0;
  for (let i = 1; i <= nums.length; ++i) {
    ans ^= i ^ nums[i - 1];
  }
  return ans;
};

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

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

发布评论

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