Search Insert Position——LeetCode
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
题目大意:给一个有序数组(无重复元素),一个数字,检查数字是否出现在数组中,出现就返回数组下标,否则返回应插入的位置下标。
解题思路:二分。
public class Solution {
public int searchInsert(int[] nums, int target) {
if(nums==null||nums.length==0){
return 0;
}
int low = 0,high = nums.length-1;
while(low<=high){
int mid = (low+high)>>1;
if(nums[mid]==target){
return mid;
}
if(nums[mid]>target){
high=mid-1;
}else{
low=mid+1;
}
}
return low;
}
}
Search Insert Position——LeetCode的更多相关文章
- Search Insert Position [LeetCode]
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Search Insert Position leetcode java
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)
Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
随机推荐
- Android常用组件【转】
UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...
- 重新看php数组
闲来有空,最近看php手册数组这块,对于array_values() 还是第一次接触,array_values是不保留键名,只有键值的函数,还有一个作用就是 重新索引. unset() 函数,是删除 ...
- JAAS - Document
JAAS 参考文档: JAAS Reference Guide JAAS Authentication Tutorial JAAS Authorization Tutorial LoginModule ...
- WPF MediaElement.Position属性
Position 属性定义:获取或设置媒体播放时间的当前进度位置. // // 摘要: // 通过媒体播放时获取或设置进度的当前位置. // // 返回结果: // 媒体时自以来的.默认值为 00:0 ...
- VS2015使用OSChina的git功能
好长时间没有写博了,把今天的新的记录一下. 最近开始使用vs2015,vs2015支持git平台和TF功能,因为....,我选择了OSChina的git.一开始学习的此篇文章http://my.osc ...
- 查询两个日期(时间)以内的数据,between and 或 and 连>= <=,to_date()
between and 方法 select * from kk.kkhmd where larq between(to_date('2008-9-3','yyyy-mm-dd')) and (to_d ...
- SGU 146.The Runner
时间限制:0.25s 空间限制:4M 题意: 一个人在一个周长为L的圆上跑,每个时间段(Ti)的速度(Vi)不一样,问最后他离起点的圆弧距离,周长是个有四位小数的浮点数,其它全是整数. Solutio ...
- laravel4通过控制视图模板路劲来动态切换主题
通过控制视图模板路劲来动态切换主题 App::before(function($request) { $paths = Terminal::isMobile() ? array(__dir__.'/v ...
- Python直接迭代序列比通过索引迭代序列快。
小脚本跑一下看看时间. 原理:直接迭代序列是通过Python内置的迭代器去实现的,而如果迭代序列需要先造一个可迭代的序列出来.内置的迭代器并不是一下将所有的数据放入内存中,而是需要多少取多少. #!/ ...
- Java 可变参数
java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理.注意:可变参数必须位于最后一项.当可变参数个数多余一个时,必将有一个不是最后一项,所以只支持 ...