当前位置:网站首页>LeetCode(剑指 Offer)- 53 - II. 0~n-1中缺失的数字
LeetCode(剑指 Offer)- 53 - II. 0~n-1中缺失的数字
2022-08-02 13:13:00 【放羊的牧码】
题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 字节跳动
AC 代码
- Java
// 解决方案(1)
class Solution {
public int missingNumber(int[] nums) {
int l = 0, r = nums.length - 1;
while (l <= r) {
int m = (l + r) / 2;
if (nums[m] == m) {
l = m + 1;
} else if (nums[m] > m) {
r = m - 1;
} else {
return m;
}
}
return l; // 缺失的是最后一个数字 + 1, 正好是 l 的值
}
}
// 解决方案(2)
class Solution {
public int missingNumber(int[] nums) {
int i = 0, j = nums.length - 1;
while(i <= j) {
int m = (i + j) / 2;
if(nums[m] == m) i = m + 1;
else j = m - 1;
}
return i;
}
}- C++
class Solution {
public:
int missingNumber(vector<int>& nums) {
int i = 0, j = nums.size() - 1;
while(i <= j) {
int m = (i + j) / 2;
if(nums[m] == m) i = m + 1;
else j = m - 1;
}
return i;
}
};边栏推荐
- 数值的整数次方
- pytorch model to tensorflow model
- Do you know Dijkstra of graph theory?
- 【typescript】使用antd中RangePicker组件实现时间限制 当前时间的前一年(365天)
- 图文短视频自媒体怎么创作?如何让点击量达到10W?
- SQL Server 2019 installation error 0 x80004005 service there is no timely response to the start or control request a detailed solution
- this的绑定指向详细解答
- [typescript] Use the RangePicker component in antd to implement time limit the previous year (365 days) of the current time
- Redis all
- RestTemplate use: set request header, request body
猜你喜欢
随机推荐
Name conventions in FreeRTOS
GCC版本升级到指定版本
Redis all
永远退出机器学习界!
php - the first of three solid foundations
ETL(二):表达式组件的使用
使用Amazon SageMaker 构建基于自然语言处理的文本摘要应用
Good shooting js game source code
First acquaintance of scrapy framework 1
【C语言】虐打循环结构练习题
Oracle数据库的闪回技术
百日刷题计划 ———— DAY1
C语言结构体(入门)
【C语言】细品分支结构——switch语句
sql concat() function
数值的整数次方
Ribbon负载均衡的深度分析和使用
无线振弦采集仪远程修改参数方式
Oracle update误操作单表回滚
Oracle update error operation single table rollback









