当前位置:网站首页>[sword finger offer] interview question 53- Ⅱ: missing numbers in 0 ~ n-1 - binary search
[sword finger offer] interview question 53- Ⅱ: missing numbers in 0 ~ n-1 - binary search
2022-07-27 15:52:00 【Jocelin47】
Method 1 : Dichotomy search
class Solution {
public:
int missingNumber(vector<int>& nums) {
int left = 0;
int right = nums.size() - 1;
while( left <= right )
{
int mid = left + ( right - left ) / 2;
if( nums[mid] == mid )
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return left;
}
};
Method 2 : Exclusive or
0-n-1 Number , And 0-n XOR can get the final result
class Solution {
public:
int missingNumber(vector<int>& nums) {
int flag = 0;
for( int i = 0; i < nums.size(); i++ )
{
flag ^= nums[i];
}
for( int i = 0; i <= nums.size(); i++)
{
flag ^= i;
}
return flag;
}
};
Method 3 :0-n Add subtract the number in the array
边栏推荐
- 使用Lombok导致打印的tostring中缺少父类的属性
- CAS compares the knowledge exchanged, ABA problems, and the process of lock upgrading
- [Yunxiang book club issue 13] coding format of video files
- Interview focus - TCP protocol of transport layer
- Troubleshooting the slow startup of spark local programs
- 【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
- Division of entity classes (VO, do, dto)
- The risk of multithreading -- thread safety
- Summary of network device hard core technology insider router (Part 2)
- On juicefs
猜你喜欢
随机推荐
Go language slow start - Basic built-in types
[正则表达式] 匹配多个字符
Push down of spark filter operator on parquet file
C: On function
折半查找
The risk of multithreading -- thread safety
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
面试重点——传输层的TCP协议
网络原理(1)——基础原理概述
Binder initialization process
synchronized和ReentrantLock的区别
数据表的约束以及设计、联合查询——8千字攻略+题目练习解答
Use double stars instead of math.pow()
Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
[Yunxiang book club issue 13] packaging format and coding format of audio files
What format is this data returned from the background
flutter —— 布局原理与约束
JS uses extension operators (...) to simplify code and simplify array merging
After the table is inserted into an in row formula, the cell loses focus
Network principle (1) - overview of basic principles









