当前位置:网站首页>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.
边栏推荐
- Why can't the start method be called repeatedly? But the run method can?
- Define MySQL function to realize multi module call
- Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
- All things work together, and I will review oceanbase's practice in government and enterprise industry
- How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
- leetcode 650. 2 keys keyboard with only two keys (medium)
- 开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- Hit the industry directly! The propeller launched the industry's first model selection tool
- Returns the maximum distance between two nodes of a binary tree
- Speech recognition Series 1: speech recognition overview
猜你喜欢

Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track

Fusion de la conversion et de la normalisation des lots

Remote connection of raspberry pie by VNC viewer

How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base

数据集-故障诊断:西储大学轴承的各项数据以及数据说明
![[shutter] shutter open source project reference](/img/3f/b1d4edd8f8e8fd8e6b39548448270d.jpg)
[shutter] shutter open source project reference

CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)

MySQL Foundation

Digital collection trading website domestic digital collection trading platform

List of major chip Enterprises
随机推荐
Difference between NVIDIA n card and amda card
Integration of revolution and batch normalization
[live broadcast appointment] database obcp certification comprehensive upgrade open class
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
Bean加载控制
Interface automation coverage statistics - used by Jacobo
接口自动化覆盖率统计——Jacoco使用
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
SQL query statement parameters are written successfully
可知论与熟能生巧
附加:token;(没写完,别看…)
Use redis to realize self increment serial number
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
Explain in detail the process of realizing Chinese text classification by CNN
Hit the industry directly! The propeller launched the industry's first model selection tool
Returns the root node of the largest binary search subtree in a binary tree
Request and response
Yolox enhanced feature extraction network panet analysis
What experience is there only one test in the company? Listen to what they say
S12. Verify multi host SSH mutual access script based on key