当前位置:网站首页>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;
}
}
边栏推荐
- Acticiti中startProcessInstanceByKey方法在variable表中的如何存储
- How to sinicize the JMeter interface?
- Solution to Dlib installation failure
- MQ set expiration time, priority, dead letter queue, delay queue
- pytorch中几个难理解的方法整理--gather&squeeze&unsqueeze
- B1022 a+b in d-ary
- 听过最自律的一句话: 那些我难以言表 不作声响
- What should test / development programmers over 35 do? Many objective factors
- Differences among left join, inner join and right join
- LeetCode之6 ZigZag Conversion
猜你喜欢

JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位

JVM Part 1: memory and garbage collection part 12 -- stringtable

pyside2____1.安装和案列

A math problem cost the chip giant $500million

Bean's life cycle & dependency injection * dependency auto assembly

JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter

2、 MySQL advanced

Message reliability processing

Database design - relational data theory (ultra detailed)

What should test / development programmers over 35 do? Many objective factors
随机推荐
LeetCode刷题之322 Coin Change
辗转相除法
Introduction to dynamic memory functions (malloc free calloc realloc)
B1023 组个最小数
枚举类实现单例模式
B1028 census
Bean的生命周期&&依赖注入*依赖自动装配
Alphabetic order problem
弹球小游戏
B1025 reverse linked list*******
Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
B1027 print hourglass
What should test / development programmers over 35 do? Many objective factors
ssm框架整合
Create datasource using Druid connection pool
The provision of operation and maintenance manager is significantly affected, and, for example, it is like an eep command
redis集群
稀疏数组→五子棋的存盘续盘等操作
JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
Two dimensional array summation exercise