当前位置:网站首页>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.
边栏推荐
- 直击产业落地!飞桨重磅推出业界首个模型选型工具
- What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
- Analyze ad654: Marketing Analytics
- 【OJ】两个数组的交集(set、哈希映射 ...)
- Yolox enhanced feature extraction network panet analysis
- Many to one, one to many processing
- [error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
- What experience is there only one test in the company? Listen to what they say
- [shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
- Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
猜你喜欢
Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
Integration of revolution and batch normalization
Many to one, one to many processing
2022 latest and complete interview questions for software testing
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
采用VNC Viewer方式遠程連接樹莓派
JSON数据传递参数
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
Writing of head and bottom components of non routing components
随机推荐
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
S12. Verify multi host SSH mutual access script based on key
Go basic data type
JDBC練習案例
How to set automatic reply for mailbox and enterprise mailbox?
JDBC教程
35 pages dangerous chemicals safety management platform solution 2022 Edition
Detailed explanation of 'viewpager' in compose | developer said · dtalk
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
I've been interviewed. The starting salary is 16K
Unique line of "Gelu"
MFC文件操作
Optimization of streaming media technology
Codeforces Round #771 (Div. 2)---A-D
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
Many to one, one to many processing
JSON数据传递参数
@How to use bindsinstance in dagger2
Define MySQL function to realize multi module call
MySQL基础