Search Insert Position 解答
Question
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
Solution 1 -- Naive
Iterate the array and compare target with ith and (i+1)th element. Time complexity O(n).
public class Solution {
public int searchInsert(int[] nums, int target) {
if(nums==null) return 0;
if(target <= nums[0]) return 0;
for(int i=0; i<nums.length-1; i++){
if(target > nums[i] && target <= nums[i+1]){
return i+1;
}
}
return nums.length;
}
}
Solution 2 -- Binary Search
If the target number doesn't exist in original array, then after iteration, it must be pointed by low pointer.
Time complexity O(log(n))
public class Solution {
public int searchInsert(int[] nums, int target) {
if (nums == null || nums.length == 0)
return 0;
int start = 0, end = nums.length - 1, mid = (end - start) / 2 + start;
while (start <= end) {
mid = (end - start) / 2 + start;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
start = mid + 1;
else
end = mid - 1;
}
return start;
}
}
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][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 ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- 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-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
随机推荐
- C++函数后面加const修饰
声明一个成员函数的时候用const关键字是用来说明这个函数是 "只读(read-only)"函数,也就是说明这个函数不会修改任何数据成员(object). 为了声明一个const成 ...
- 【转】基于Qt, TUIO和TSLIB的嵌入式Linux下的多点触摸设计
这个教程描述了在嵌入式linux下使用Qt如何设置一个支持多点触摸和单点触摸的输入系统.这里假定你已经有了对应的驱动程序,驱动可以从触摸屏的厂商那里获得或者使用一个linux 内核源码中已经存在的驱动 ...
- 基础总结篇之二:Activity的四种launchMode
合抱之木,生於毫末:九層之台,起於累土:千里之行,始於足下.<老子> 今天在社区看到有朋友问“如何在半年内成为顶级架构师”,有网友道“关灯睡觉,不用半年的...”,的确,做梦还来的快一些. ...
- SlipButton——滑动开关
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjI1MjUwMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Hacker(十一)----黑客常用入侵方法
Internet中,为了防止黑客入侵自己的电脑,就必须了解黑客入侵目标计算机的常用方法.黑客常用的入侵方法有数据驱动攻击.系统文件非法利用.伪造信息攻击.远端操纵等. 一.数据驱动攻击 数据驱动攻击是 ...
- Microsoft Visual C++ 不支持long long
Microsoft Visual C++ 不支持long long 在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异.一般来说, ...
- 图片翻页效果引出的animate.css,很好玩,多动动吧~
有一个项目,客户需要页面翻转的效果,需要应用在合作伙伴里面的图片上,一共有43张图片,我把它做成了随机定时的转动,鼠标经过时转动: animate.css科普文章:http://www.dowebok ...
- 文本框脚本 - select 事件
HTML中,用两种方式来表示文本框: input 单行文本.textarea 多行文本 那么在文本中存在哪些事件尼? 1 select 都支持 但是其触发的时机不一样 IE9+ .Safair ...
- jquery css3 手机菜单动画综合版
html <header> <a id="go-back" href="javascript:window.location.back(-1)" ...
- SELECT TOP column FROM table [ORDER BY column [DESC]]
如果想返问表中行的子集,仅需要返回特定数量的记录,而不管符合条件的行有多少.要返回排在前面的值,可以有两个选择:指定固定数量的行,或者指定总行数的百分比.SQL Server不对这些数据做任何分析,共 ...