当前位置:网站首页>【剑指offer】面试题53-Ⅱ:0~n-1中缺失的数字——二分查找
【剑指offer】面试题53-Ⅱ:0~n-1中缺失的数字——二分查找
2022-07-27 14:24:00 【Jocelin47】
方法一:二分查找法
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;
}
};
方法二:异或
0-n-1个数,与0-n异或可以得到最终的结果
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;
}
};
方法三:0-n相加减去数组中的数
边栏推荐
- shell脚本读取文本中的redis命令批量插入redis
- STM32学习之CAN控制器简介
- js使用for in和for of来简化普通for循环
- The first common node of the two linked lists of "Jianzhi offer"
- “router-link”各种属性解释
- md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
- MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
- js使用一元运算符简化字符串转数字
- 基于stm32的数字示波器设计方案
- Spark 3.0 测试与使用
猜你喜欢

实现自定义Spark优化规则

How to take satisfactory photos / videos from hololens

Spark Filter算子在Parquet文件上的下推

4种单片机驱动继电器方案

npm install错误 unable to access

STM32 CAN 通信 滤波设置问题

Leetcode 74. search two-dimensional matrix bisection /medium

With just two modifications, apple gave styleganv2 3D generation capabilities

Dan bin Investment Summit: on the importance of asset management!

Leetcode-1737- minimum number of characters to change if one of the three conditions is met
随机推荐
MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
后台返回来的是这种数据,是什么格式啊
Network equipment hard core technology insider router Chapter 14 from deer by device to router (middle)
Several basic uses of tl431-2.5v voltage reference chip
Spark Filter算子在Parquet文件上的下推
Comparison of advantages and disadvantages between instrument amplifier and operational amplifier
Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
Overview of wechat public platform development
STL value string learning
Sword finger offer merges two sorted linked lists
USB2.0接口的EMC设计方案
Basic usage of kotlin
STM32 can communication filter setting problem
Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
Leetcode 90. subset II backtracking /medium
Unity's simplest object pool implementation
4种单片机驱动继电器方案
Unity3d learning note 10 - texture array
华为鸿蒙模拟器去除顶部导航栏方法
Leetcode-1737-满足三条件之一需改变的最少字符数