[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 ...
随机推荐
- 我利用网上代码开发的JQuery图片插件
我利用网上代码开发的JQuery图片插件 代码如下 (function($){ $.fn.FocusPic = function(options){ var defaults = { interval ...
- dos2unix unix2dos
实现windows和linux之间的文件自动转换,消除^M.
- 如何用Tacker将NFV带入OpenStack?
最初社区里很多人争论过NFV是否属于OpenStack,而后来可以确定的是OpenStack的确占据了NFV会话中的很大一部分,并且形象地反映在了下面的ETSI MANO概念架构图中,OpenStac ...
- The import java.io cannot be resolved
在导入一个新项目后出现 The import java.io cannot be resolved.String cannot be resolved to a type 解决: 将JRE Syste ...
- Mac下U盘安装系统“未验证的错误”
bash下 输入下面命令: date 1220141012015.30
- TaskScheduler的启动
<深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...
- PS:蓝天白云的制作
方法一: 1.新建(ctrl+N),根据自己的需求设置画面大小: 2.设置前景色为蓝天颜色,alt+delete,填充为天空蓝颜色: 3.滤镜-渲染-云彩,得出蓝天白云效果: 4.根据需求再调整亮度. ...
- Nodejs在centos下的安装
新学了nodejs,发现在centos下面安装的时候,与windows有一些不同的地方,以前习惯在安装不上的时候,去百度出来,解决了以后,当时都记住如何解决的了,但是过了一段时间以后,就全都忘记光了, ...
- LeetCode OJ-- 二战 Combinations
在1 - 10 中,求出 7 个数的排列组合. 出现了超时,而超时的原因是有好多重复情况,复杂度上来说,和答案的复杂度是一样的,但是答案中重复了太多了,体会下. 超时1: class Solution ...
- Spark难道比oracle性能还差?百万级数据测试性能
本人对大数据方面也是刚刚研究,由于工作需要在实时查询与统计的性能方面要深入学习.现测试性能如下: 环境:VirtualBox host-only ubuntu版本: Linux master 4 ...