[LeetCode] 35. Search Insert Position ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5
Output: 2
Example 2:
Input: [1,3,5,6], 2
Output: 1
Example 3:
Input: [1,3,5,6], 7
Output: 4
Example 4:
Input: [1,3,5,6], 0
Output: 0
//mid+1,mid-1,是为了防止有重复数据,可能死循环
public int searchInsert(int[] A, int target) {
int low = 0, high = A.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (A[mid] == target)
return mid;
else if (A[mid] > target)
high = mid - 1;
else
low = mid + 1;
}
return low;
}
[LeetCode] 35. Search Insert Position ☆(丢失的数字)的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35. Search Insert Position (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- [LeetCode] 35. Search Insert Position 解决思路
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [leetcode]35. Search Insert Position寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Java [leetcode 35]Search Insert Position
题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
随机推荐
- bzoj1642 / P2889 [USACO07NOV]挤奶的时间Milking Time
P2889 [USACO07NOV]挤奶的时间Milking Time 普通的dp 休息时间R其实就是把结束时间后移R个单位而已.但是终点也需要后移R位到n+R. 每个时间段按起始时间排序,蓝后跑一遍 ...
- P2512 [HAOI2008]糖果传递&&P3156 [CQOI2011]分金币&&P4016 负载平衡问题
P2512 [HAOI2008]糖果传递 第一步,当然是把数据减去平均数,然后我们可以得出一串正负不等的数列 我们用sum数组存该数列的前缀和.注意sum[ n ]=0 假设为链,那么可以得出答案为a ...
- troubleshooting-执行导数shell脚本抛异常error=2, No such file or directory
Cannot run program "order_log.sh" (in directory "/data/yarn/nm/usercache/chenweidong/ ...
- 09:Linux 中各个文件夹的作用
参考博客 / 根目录 包含了几乎所的文件目录.相当于中央系统.进入的最简单方法是:cd /. /boot 引导程序,内核等存放的目录 这个目录,包括了在引导过程中所必需的文件.在最开始的启动阶段, ...
- 使用CloudFlare 的 PKI 工具集 cfssl 来生成 Certificate Authority (CA) 证书和秘钥文件
要安装kubernetes最新版集群,https://github.com/opsnull/follow-me-install-kubernetes-cluster 这个文档必须要研习一下了. 以下实 ...
- 20145332卢鑫 WEB基础
20145332卢鑫 WEB基础 实验过程 1.环境配置 2.简单的网页编写 3.javascript相关 1.相关概念:JavaScript是一种广泛用于客户端Web开发的脚本语言,常用来给HTML ...
- cogs 362. [CEOI2004]锯木厂选址
★★★ 输入文件:two.in 输出文件:two.out 简单对比 时间限制:0.1 s 内存限制:32 MB 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来. ...
- 使用InputStreamReader读入,使用OutputStreamWriter写出,将一首诗按行重写?
https://www.processon.com/view/link/5b1a3880e4b00490ac8f5f40 改善后: (可将不管一行有几个字时的不规律的文本,按行倒写) package ...
- bootstrap栅格系统进行偏移格式
本文为博主原创,转载请注明出处: offset偏移都是向右偏移,且只能向右偏移,例: col-md-offset-2,向右偏移两列. col-md-pull-偏移数值 //向左偏移 c ...
- UVa 1632 阿里巴巴(区间DP)
https://vjudge.net/problem/UVA-1632 题意: 直线上有n个点,其中第i个点的坐标是xi,且它会在di秒之后消失.Alibaba可以从任意位置出发,求访问完所有点的最短 ...