当前位置:网站首页>Missing number
Missing number
2022-07-03 00:02:00 【wx58c8fa5d0b356】
Missing number
subject
Given an inclusion 0, 1, 2, …, n in n Sequence of Numbers , find 0 … n The number that does not appear in the sequence .
Example
Example 1:
Input : [3,0,1]
Output : 2
Example 2:
Input : [9,6,4,2,3,5,7,0,1]
Output : 8
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
Problem solving
Ideas
There are two simple and feasible schemes , One is to use bit operations , One is to solve problems with mathematical methods
- An operation
# The algorithm of XOR bit operation
1. a ⊕ a = 0
2. a ⊕ b = b ⊕ a
3. a ⊕b ⊕ c = a ⊕ (b ⊕ c) = (a ⊕ b) ⊕ c;
4. d = a ⊕ b ⊕ c Can be launched a = d ⊕ b ⊕ c.
5. a ⊕ b ⊕ a = b.
If the length of the array is n, So it's actually asking (0 - n.length) The missing number in the range
We put (0 - n.length) All numbers XOR up nums All values in the array , Finally, it is actually the solution sought .
Because two identical numbers XOR equals 0,0 I wonder if any number on the list is still that number .
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- Mathematical methods
We put (0 - n.length) Sum all the numbers , And then subtract it in turn nums Values in the array , The final result is what you want .
- 1.
Time complexity is O(n), The space complexity is O(1)
Code
- An operation
class Solution {
public int missingNumber(int[] nums) {
int sum = ((nums.length) * (nums.length + 1)) / 2;
for (int num : nums) {
sum -= num;
}
return sum;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- Mathematical methods
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;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
边栏推荐
- 【OJ】两个数组的交集(set、哈希映射 ...)
- leetcode 650. 2 keys keyboard with only two keys (medium)
- Flexible combination of applications is a false proposition that has existed for 40 years
- Additional: token; (don't read until you finish writing...)
- JDBC practice cases
- Interface difference test - diffy tool
- 判断二叉树是否为满二叉树
- Interface switching based on pyqt5 toolbar button -1
- What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
- 秒杀系统设计
猜你喜欢
![Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]](/img/d8/d22cbbaccb1594ee46aca098c41002.png)
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
![[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)](/img/c5/2f65d37682607aab98443d7f1ba775.jpg)
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)

Why can't the start method be called repeatedly? But the run method can?

Hit the industry directly! The propeller launched the industry's first model selection tool

Optimization of streaming media technology

Additional: token; (don't read until you finish writing...)

How QT exports data to PDF files (qpdfwriter User Guide)

Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?

Writing of head and bottom components of non routing components

容器运行时分析
随机推荐
数据集-故障诊断:西储大学轴承的各项数据以及数据说明
SQL query statement parameters are written successfully
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Unique line of "Gelu"
Flexible combination of applications is a false proposition that has existed for 40 years
Integration of revolution and batch normalization
Load balancing cluster (LBC)
返回二叉树中最大的二叉搜索子树的根节点
leetcode 650. 2 keys keyboard with only two keys (medium)
程序分析与优化 - 9 附录 XLA的缓冲区指派
判断二叉树是否为满二叉树
Implementation of VGA protocol based on FPGA
[reading notes] phased summary of writing reading notes
Custom throttling function six steps to deal with complex requirements
MySQL基础
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
Digital twin smart factory develops digital twin factory solutions