当前位置:网站首页>268.missing number of leetcode
268.missing number of leetcode
2022-07-27 05:23:00 【Master yuan】
List of articles
1. Description
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
2. Follow Up
Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
3. Test Cases
Example 1:
Input: nums = [3,0,1]
Output: 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:
Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Example 3:
Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
Example 4:
Input: nums = [0]
Output: 1
Explanation: n = 1 since there is 1 number, so all numbers are in the range [0,1]. 1 is the missing number in the range since it does not appear in nums.
4. Solution
4.1
If you don't think of any strange tricks , Then write an answer honestly first .
class Solution {
public int missingNumber(int[] nums) {
Set<Integer> numSet = new HashSet<Integer>();
for(int num : nums) {
numSet.add(num);
}
int expectedNumCount = nums.length + 1;
for (int number=0; number<expectedNumCount; number++) {
if (!numSet.contains(number)) {
return number;
}
}
return -1;
}
}
The efficiency of this is obviously not high .
4.3
Here you can use special skills .
XOR operation is a magical operation in computer , It can be simply understood as the same as 0, Different for 1. If you use 0 And any number XOR , Then the result is any number .
here , Because we know that only one number is missing in the array , And the size of the numerical value is the length of the array +1 The size of is the maximum . Then we can see the array [3,0,1], The array length is 3, The corresponding number is 0,1,2,3 Four Numbers , But one is missing . Consider the corresponding subscript , Respectively 0,1,2, In this way, we can use the subscript of the array and the exclusive or of the array word , More than the length of the array +1, namely
3 ^ 0 ^ 3 ^ 1 ^ 0 ^ 2 ^ 1 = 2, That is, what is missing is 2.
class Solution {
public int missingNumber(int[] nums) {
int missing = nums.length;
for (int i = 0; i < nums.length; i++) {
missing ^= i ^nums[i];
}
return missing;
}
}
边栏推荐
- SSM framework integration
- During its low-level period, this slave edge causes the instruction number to make a corresponding model
- B1026 program running time
- LeetCode之6 ZigZag Conversion
- Explore the mysteries of the security, intelligence and performance of the universal altek platform!
- JVM Part 1: memory and garbage collection part 3 - runtime data area - overview and threads
- 使用Druid连接池创建DataSource(数据源)
- B1029 old keyboard
- 素数筛选(埃氏筛法,区间筛法,欧拉筛法)
- B1026 程序运行时间
猜你喜欢

JVM Part 1: memory and garbage collection part 5 -- runtime data area virtual machine stack

JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
![[CSAPP] Application of bit vectors | encoding and byte ordering](/img/96/344936abad90ea156533ff49e74f59.gif)
[CSAPP] Application of bit vectors | encoding and byte ordering

JVM上篇:内存与垃圾回收篇二--类加载子系统

数据库设计——关系数据理论(超详细)

ERP system brand
![[Niuke discussion area] Chapter 7: building safe and efficient enterprise services](/img/62/2b170e8dd5034708aebc6df0bc2b06.png)
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services

35. Scroll

How to test the payment process?

BIO、NIO、AIO区别
随机推荐
LeetCode刷题之322 Coin Change
JVM Part 1: memory and garbage collection part 10 - runtime data area - direct memory
Explore the mysteries of the security, intelligence and performance of the universal altek platform!
Three waiting methods of selenium and three processing methods of alert pop-up
OFDM 16 lecture 2-ofdm and the DFT
集合框架的使用
B1031 查验身份证
Select user stories | the false positive rate of hole state in jushuitan is almost 0. How to do this?
Shell course summary
Introduction to Kali system ARP (network disconnection sniffing password packet capturing)
B1023 组个最小数
SSM framework integration
I've heard the most self disciplined sentence: those I can't say are silent
How to test the payment process?
使用Druid连接池创建DataSource(数据源)
2021 OWASP top 6-10 collection
Interface and abstract class / method learning demo
B1028 census
mq常见问题
B1028 人口普查