[leetcode 35] Search Insert Position
1 题目:
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.
Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0
2 思路:
变相的二分搜索
注意处理边界和溢出情况
3 代码:
public int searchInsert(int[] nums, int target) {
// pre handle
if(nums[0] > target){
return 0;
}
if(nums[nums.length-1] < target){
return nums.length;
}
/* again, binary search */
int pre = 0;
int after = nums.length-1;
while(pre<=after){
int mid = pre + ((after-pre)>>1);
if(nums[mid]==target){
return mid;
}else if(nums[mid]<target){
if(mid < nums.length-1 && nums[mid+1]>target){
return mid+1;
}
pre = mid+1;
}else if (nums[mid]>target){
if(mid>0 && nums[mid-1]<target){
return mid;
}
after = mid-1;
}
}
// will never reach here
return 0;
}
[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 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数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 ...
- [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 ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路 Given a sorted array ...
- 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 ...
随机推荐
- CSS浮动布局与菜单栏设计
公司周六停电,终于可以双休了.用周五空余时间再夯实一下css基础,<CSS权威指南>概念性的内容看起来容易犯困,不如实践来得快,动手操作吧. 一.浮动布局 浮动存在问题:浮动使元素脱离文档 ...
- jqueyr获取动态创建的元素
javascript代码是按照代码顺序执行的,所以如果你用某个元素的click事件创建一个元素,id为test,然后在下面$("#test")是无法选择到的. 正确的方法是将在cl ...
- 【Leetcode-Mysql】Trips and Users
思路不总结了,看过题目自己尝试过之后,看下方代码应该能理解的 SELECT Request_at AS DAY, round( sum( CASE WHEN STATUS = 'completed' ...
- M2事后分析汇报总结
学霸网站项目Postmortem结果 M2之于M1的改进 文档和问答的整合 完成webservice 完成数据库触发器设计与完整性约束依赖(大规模) 优化学霸UI 资源的搜索 外部问题的搜索 文档的上 ...
- js继承
js继承有5种实现方式: 继承第一种方式:对象冒充 function Parent(username){ this.username = username; this.hello = function ...
- Ajax Step By Step5
第五.[表单序列化] Ajax 用的最多的地方莫过于表单操作,而传统的表单操作是通过 submit 提交将数据传 输到服务器端.如果使用 Ajax 异步处理的话,我们需要将每个表单元素逐个获取才方能提 ...
- MySql之安装配置
1.解压Mysql.zip后,添加bin目录的环境变量 2.配置my.ini文件中的 # basedir =D:\mysql-5.6.24-winx64 # datadir = D:\mysql-5. ...
- First class ,6 examples anlaysisi
http://www.fgm.cc/learn/ First class ,6 examples anlaysisi <!DOCTYPE html> <!-- To change t ...
- 【转】linux yum命令详解
yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...
- Qt Sqlite qwt 发布过程中碰到的问题runtime error
qt版本:4.8.0 qwt版本:6.1.2 使用dll show检测缺少的dll,或者笨一点的方法,点击运行差什么找什么放进去: 左上显示exe调用哪些dll,右边是dll又再次调用啦哪些dll: ...