[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 ...
随机推荐
- java多线程详解(5)-Threadlocal用法
ThreadLocal是什么 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路. 使用这个工具类可以很简洁 ...
- ListView显示不同行以及数据重用
Handling ListViews with Multiple Row Types When you start writing Android Apps it isn’t long before ...
- bootstrap-datepicker使用
$('.date').datepicker({ language: 'zh-CN', --指定格式 format: 'yyyy-mm', --格式要求 autoclose: true, --选择后是否 ...
- Python中实现异步并发查询数据库
这周又填了一个以前挖下的坑. 这个博客系统使用Psycopy库实现与PostgreSQL数据库的通信.前期,只是泛泛地了解了一下SQL语言,然后就胡乱拼凑出这么一个简易博客系统. 10月份找到工作以后 ...
- 云计算P2V的迁移过程
总结一下客户在传统物理机向虚拟化环境迁移中的典型场景和常用工具及步骤.
- I/O知识
1.jdk1.4之前(jdk1.4开始提供了nio)的早起版本,java对I/O的支持并不完善,开发人员开发高性能I/O程序时,面临的问题主要有: 没有缓冲区,I/O性能存在问题 没有 ...
- js继承
js继承有5种实现方式: 继承第一种方式:对象冒充 function Parent(username){ this.username = username; this.hello = function ...
- javascript总结
javascript:它是一种script脚本语言 脚本语言:就是可以和HTML混合在一起使用的语言,可以用来在IE的客 户端进行程序编制,从 ...
- jekyll安装的斗智斗勇
jekyll---将纯文本转化为静态网站和博客,GitHub Pages 可以运行 Jekyll,你很简单就可以完全免费的在 GitHub 上发布网站. 小白安装jekyll时的若干问题,有错误欢迎指 ...
- hdoj 1596 find the safest rode
Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条 ...