当前位置:网站首页>Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
2022-07-27 11:48:00 【Ignorant little nine】
T53 - II. 0~n-1 Missing numbers in
A length of n-1 All numbers in the incremental sort array of are unique , And every number is in the range 0~n-1 within . In scope 0~n-1 Internal n There are and only one number is not in the array , Please find out the number .
Example 1:
Input : [0,1,3]
Output : 2
Example 2:
Input : [0,1,2,3,4,5,6,7,9]
Output : 8
Limit :
1 <= The length of the array <= 10000
Ideas
Two points , Replacement in place , See if it corresponds to that number
solution 1
class Solution {
public int missingNumber(int[] nums) {
int left =0, right =nums.length-1;
while(left<=right){
int mid = left+((right-left)>>1);
if(nums[mid]>mid){
right = mid-1;
}else{
left = mid+1;
}
}
return left;
}
}
0 ms, In all Java Defeated in submission **100.00%** Users of
Memory consumption :39.1 MB, In all Java Defeated in submission **26.28%** Users of
solution 2
class Solution {
public int missingNumber(int[] nums) {
int left =0, right =nums.length-1;
while(left<=right){
int mid = left+((right-left)>>1);
if(nums[mid]==mid){
left = mid+1;
}else{
right = mid -1;
}
}
return left;
}
}
Execution time :0 ms, In all Java Defeated in submission **100.00%** Users of
Memory consumption :38.9 MB, In all Java Defeated in submission **59.84%** Users of
Time complexity :O(logn)
Spatial complexity :O(1)
边栏推荐
- Maker Hongmeng application development training notes 02
- Interaction free shell programming
- Moveit2 - 1. Start
- Summary of C language knowledge involved in learning STM32F103 (link only)
- 求不同采样周期下的传递函数有限零点
- Summary of leetcode SQL exercises (MySQL Implementation)
- Moveit2 -- 2. Quick start of moveit in rviz
- makefile模板
- 第7章 异常处理
- 第10章 枚举类与注解
猜你喜欢

Maker Hongmeng application development training 04

What is private traffic?

Analysis of the use of JUC framework from runnable to callable to futuretask

Regular expression of shell programming (grep of shell script text three swordsmen)

Shell脚本文本三剑客之sed

Modelarts image classification and object detection
新版数据仓库的同步使用参考(新手向)

Principle of control system based on feedback rate

N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei

Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
随机推荐
Matlab S-function详解
Greek alphabet reading
Local virtual machine initialization script
[machine learning whiteboard derivation series] learning notes --- conditional random fields
The difference between microcomputer and single chip microcomputer
Newton Raphson iterative method
[special topic] summary of RMQ question brushing with ST table
C# 自定义集合
The C programming language-2nd-- notes -- 4.11.3
Solution of digital tube flash back after proteus8 professional version cracking
zabbix自定义监控项
Analysis of the use of JUC framework from runnable to callable to futuretask
LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
开源flink有写holo的Datastream connector或者flink sql conn
【Unity入门计划】CreatorKitFPS:第一人称射击3D小游戏
剑指 Offer 笔记: T57 - II. 和为 s 的连续正数序列
CH340模块无法识别/烧写不进的一种可能性
C programming language (2nd Edition) -- Reading Notes -- 1.5.1
Difference quotient approximation of wechat quotient
[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution