当前位置:网站首页>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;
}
};
边栏推荐
- How to implement waterfall flow layout (what is waterfall flow layout)
- 图论之Prim,最小生成树该怎么解?
- Seata Distributed Transaction
- Article 48 - Analysis of timestamp2 parameters【2022-08-01】
- Mysql index details (with pictures and texts)
- 【C语言】剖析函数递归(3)
- 【C语言】函数哪些事儿,你真的get到了吗?(2)
- Interpretation of new features | MySQL 8.0 GIPK invisible primary key
- How to turn off hardware acceleration [easy to understand]
- Mysql索引详解(图文并茂)
猜你喜欢
Introduction to Scala Basic Syntax (3) Various Operators in Scala
Enterprise Network Planning Based on Huawei eNSP
How to connect DBeaver TDengine?
删除链表的节点
Good shooting js game source code
【C语言】细品分支结构——if-else语句
嵌入式系统驱动初级【2】——字符设备驱动基础上_基础框架
How to use the database like tap water?|Tencent Cloud Database TDSQL-C
图论之Floyd,多源图最短路如何暴力美学?
你知道图论的spfa吗?
随机推荐
RestTemplate 使用:设置请求头、请求体
【C语言】剖析函数递归(2)
冰箱“扩容”的战事,在今夏格外猛烈
.Net 5.0 Quick Start Redis
Based on the flask mall administrator functions
如何通过DBeaver 连接 TDengine?
【C语言】虐打循环练习题(2)
短视频美食自媒体怎么做?5步教你快速上手
无线振弦采集仪远程修改参数方式
RISC-V instruction format and 6 basic integer instructions
我的创作纪念日
Enterprise Network Planning Based on Huawei eNSP
【C语言】细品分支结构——if-else语句
自动生成代码器推荐-code-gen
GCC版本升级到指定版本
First acquaintance of scrapy framework 1
Seata分布式事务
Set proxy server (Google+IE) "Recommended Collection"
String concatenation in SQL
k8s之KubeSphere部署有状态数据库中间件服务 mysql、redis、mongo