题目:

搜索插入位置

给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。

你可以假设在数组中无重复元素。

样例

[1,3,5,6],5 → 2

[1,3,5,6],2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6],0 → 0

解题:

二分法直接搞,找到了返回下标,找不到结束时候的start ==end就是要插入的下标,非递归程序。

Java程序:

public class Solution {
/**
* param nums : an integer sorted array
* param target : an integer to be inserted
* return : an integer
*/
public int searchInsert(int[] nums, int target) {
// write your code here
if(nums==null)
return 0;
if(nums.length==0)
return 0;
int start = 0;
int end = nums.length;
while(start<end){
int median = (start+end)/2;
if(nums[median]==target)
return median;
else if(nums[median]<target){
start = median + 1;
}else{
end = median - 1;
}
}
return start; }
}

总耗时: 1617 ms

修改一点,或者更好理解。

Python程序:

class Solution:
"""
@param A : a list of integers
@param target : an integer to be inserted
@return : an integer
"""
def searchInsert(self, nums, target):
# write your code here
if nums==None:
return 0
if len(nums)==0:
return 0
start = 0
end = len(nums)-1
while start<=end:
median = (start + end)/2
if nums[median] == target:
return median
elif nums[median] < target:
start = median + 1
else:
end = median - 1
return start

总耗时: 474 ms

lintcode:Search Insert Position 搜索插入位置的更多相关文章

  1. [LeetCode] Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. [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 ...

  3. 035 Search Insert Position 搜索插入位置

    给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...

  4. 【LeetCode】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  5. [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 ...

  6. 【LeetCode每天一题】Search Insert Position(搜索查找位置)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. Search insert position, 查找插入位置

    问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...

  9. LintCode Search Insert Position

    找出指定target的位置(没有此数时为按顺序应当位置). public class Solution { /** * param A : an integer sorted array * para ...

随机推荐

  1. 【Qt】QWidget、QDialog、QMainWindow的异同点【转】

    简述 在分享所有基础知识之前,很有必要在这里介绍下常用的窗口-QWidget.QDialog.QMainWindow. 熟悉Qt的同学都应该知道,在新建Qt Widgets项目进行类信息选择时会碰到它 ...

  2. 使用awstats分析iis站点的日志

    环境:win7 + iis7 + perl(ActivePerl-5.20.1.2000) + awstats 7.3 一.找到iis日志所在目录 建议全部都打勾 二.安装perl AWStats是p ...

  3. js中substring和substr的用法比较

    推荐使用substring 方法   stringObject.substring(start,stop)   stringObject.substr(start,length)   定义和用法 提取 ...

  4. [大牛翻译系列]Hadoop(7)MapReduce:抽样(Sampling)

    4.3 抽样(Sampling) 用基于MapReduce的程序来处理TB级的数据集,要花费的时间可能是数以小时计.仅仅是优化代码是很难达到良好的效果. 在开发和调试代码的时候,没有必要处理整个数据集 ...

  5. 使用dxNavBar动态创建应用程序菜单

    一.如何动态创建dxNavBar内容: function TMain.GetAcitonByCaption(const aCategory,aCaption: string): Integer; va ...

  6. Oracle内存管理理论篇二

    目标 了解oracle内存管理方式 掌握ASMM管理方式 掌握AMM管理方式 监控内存使用 学习一个知识点时,最好先了解其历史.ORACLE近期的版本都对内存管理做了简化,从9i通过PGA_AGGRE ...

  7. Python的循环

    循环是一个结构,导致一个程序要重复一定的次数 条件循环也一样,当条件变为假,循环结束 For循环 在python for循环遍历序列,如一个列表或一个字符. for循环语法:   ——for iter ...

  8. MySQL ibdata1撑爆占满磁盘空间

    MySQL主从由于ibdata1占满磁盘空间-->主从失效 因为设置了innodb_file_per_table = 1,ibdata1依旧撑爆占满磁盘空间 主从断的时候,IO线程在连接,SQL ...

  9. self,parent,this区别

    我容易混淆public,private,protected,还容易混淆this,self这些东西.前面已经写了一篇关于public,private,protected博文了,下面来说一下this,se ...

  10. iOS 进阶 第八天(0407)

    0407 UIPickerView.UIDatePicker和UIToolBar请参见视频和代码 pch文件 #ifdef __OBJC__ //在这里面写oc的引用,比如一些oc的头文件或者NSLo ...