题目:

搜索插入位置

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

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

样例

[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. WIN2003跳出res://C:WINDOWSsystem32mys.dll/mys.hta解决方法

    出现这个问题的时候 @echo off 请将以下语句复制到记事本中,另存为后缀为.cmd的文件,并运行.当然在命令行下一句句运行也没问题. echo 正在修复,这个过程可能需要几分钟,请稍候…… ru ...

  2. SQLServer处理亿万级别的数据的优化措施

    如何在SQLServer中处理亿万级别的数据(历史数据),可以按以下方面进行: 去掉表的所有索引 用SqlBulkCopy进行插入 分表或者分区,减少每个表的数据总量 在某个表完全写完之后再建立索引 ...

  3. python 内置模块之hashlib、hmac、uuid

    一.hashlib md5和sha算法通过消息摘要算法生成定长的消息摘要,消息摘要算法是不可逆的.但同一段消息通过摘要算法后得到的值是一样的,可一通过比对消息摘要验证数据的完整性. sha算法比MD5 ...

  4. python学习第七天

    一.        subprocess 模块 1. subprocess的介绍:用来替代几个老的模块或是函数,如:os.systam,os.popen,os.spawn*,os.popen2*,co ...

  5. 测试使用Windows Live Writer

    目前是在win7系统下面使用的,曾经在winxp下去配置,却失败了,难道不支持xp? Windows Live Writer好像不支持代码关键字高亮显示啊. int main(void) { prin ...

  6. 【转】如何在 Windows 中执行干净启动

    完成故障排除后,请执行以下步骤将计算机重置为正常启动. Windows 8.1 和 Windows 8 从屏幕右边缘滑入,然后点按“搜索”.您也可以将鼠标指向屏幕的右下角,然后单击“搜索”. 在搜索框 ...

  7. Microsoft .NET Framework 3.5 for Windowns Server2012R2 GUI

    图形化安装,需要安装盘,不需要网络连接

  8. [css]兼容性

    div +input 输入框 , 在微信中  有问题 块级元素 行内元素

  9. Linux命令练级初级

    Linux命令练级初级 http://ke.qq.com/video/index.html?course_id=6815 ls    -a 所有文件    -l    类型,连接数,所属用户,工作组, ...

  10. 在wamp下PHP5.5安装php-redis扩展

    windows下开发用的xampp集成的环境,想装个php-redis扩展,扩展的github地址: https://github.com/nicolasff/phpredis 描述里找到window ...