当前位置:网站首页>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;
}
};
边栏推荐
- 路由-Tab切换页面
- RISC-V instruction format and 6 basic integer instructions
- How to turn off hardware acceleration [easy to understand]
- 【C语言】函数哪些事儿,你真的get到了吗?(1)
- 吾爱第三课-修改版权和资源
- .Net 5.0 Quick Start Redis
- 汉源高科千兆12光12电管理型工业以太网交换机 12千兆光12千兆电口宽温环网交换机
- k8s之KubeSphere部署有状态数据库中间件服务 mysql、redis、mongo
- Scala基础语法入门(三)Scala中的各种运算符
- Introduction to Scala Basic Syntax (3) Various Operators in Scala
猜你喜欢
[C language] Explicit array solution (1)
Four seasons of trees realized by svg
"Second Uncle" is popular, do you know the basic elements of "exploding" short videos from the media?
Redis全部
FreeRTOS creation tasks - dynamic creation, static creation
[C language] Analysis of function recursion (2)
Closures in JS
数值的整数次方
【C语言】剖析函数递归(2)
js true 3d histogram plugin
随机推荐
【C语言】函数哪些事儿,你真的get到了吗?(2)
方正璞华“劳动人事法律自助咨询服务平台”在武汉武昌区投入使用!
RESTful 风格(详细介绍 + 案例实现)
In-depth analysis and use of Ribbon load balancing
How to turn off hardware acceleration [easy to understand]
Redis全部
Basic operations of openGauss database (super detailed)
How to improve the originality of self-media creation and create popular works?
基于华为eNSP的企业网络规划
PHP+MYSQL [Student Information Management System] (Minimalist Edition)
Detailed explanation of network flow (what information can the flow network diagram generally reflect)
删除链表的节点
RESTful style (detailed introduction + case implementation)
二叉树的类型、构建、遍历、操作
How to create short images and short videos from the media?How to make the click volume reach 10W?
Custom mvc framework review
How to use the database like tap water?|Tencent Cloud Database TDSQL-C
【C语言】手撕循环结构 ——do...while语句及循环练习题(1)
最小割和对偶图(未完成)
Good shooting js game source code