comments | difficulty | edit_url |
---|---|---|
true |
简单 |
数组nums
包含从0
到n
的所有整数,但其中缺了一个。请编写代码找出那个缺失的整数。你有办法在O(n)时间内完成吗?
注意:本题相对书上原题稍作改动
示例 1:
输入:[3,0,1] 输出:2
示例 2:
输入:[9,6,4,2,3,5,7,0,1] 输出:8
我们可以先对数组
否则遍历结束后,返回数组长度即可。
时间复杂度
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;
};
class Solution {
func missingNumber(_ nums: [Int]) -> Int {
let nums = nums.sorted()
for (i, x) in nums.enumerated() {
if i != x {
return i
}
}
return nums.count
}
}
我们可以先求出
时间复杂度
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;
};
class Solution {
func missingNumber(_ nums: [Int]) -> Int {
let n = nums.count
return n * (n + 1) / 2 - nums.reduce(0, +)
}
}
我们可以使用异或运算,将
时间复杂度
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;
};