题目:

搜索插入位置

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

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

样例

[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. linux的7种运行级别

    Linux有7个运行级别(runlevel)运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆运行级别2:多用户 ...

  2. How to force to Fullscreen Form

    Is it possibile by code resize a form to fullscreen? (like button Maximize) ? // VAR Changed on 10 J ...

  3. [转]Linux中find常见用法示例

    Linux中find常见用法示例[转]·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;find命令的参 ...

  4. GIS论文翻译问题

    1 在sci库中输入关键词,搜索一篇相关的英文。看看专业词汇怎么翻译。做个记录 2打开ArcGIS中文online和英文online帮助文档。在中文帮助中搜索中文。找到相应的位置,再切换到英文的版本中 ...

  5. PDF打印

    问题: Microsoft Excel 不能访问文件“D:\bwms\源代码\Dcjet.BWMS\Dcjet.Bwms.Web\PrintTmp\Check_bwms.xls”. 可能的原因有以下几 ...

  6. 【rest】 深入理解rest

    起因是想搞明白 ajax.rest风格和http请求数据会有什么区别 再来回顾一下概念: REST即表述性 状态 传递 满足这些约束条件和原则的应用程序或设计就是RESTful.需要注意的是,REST ...

  7. php 彩票类 lottery

    <?php /* * For the full copyright and license information, please view the LICENSE * file that wa ...

  8. C++ 字符串相关函数

    <转>自:http://zhidao.baidu.com/question/173202165.html 首先就是memcpy表头文件: #include <string.h> ...

  9. When to use Class.isInstance() & when to use instanceof operator?

    I think the official documentation gives you the answer to this one (albeit in a fairly nonspecific ...

  10. How to create jar for Android Library Project

    http://stackoverflow.com/questions/17063826/how-to-create-jar-for-android-library-project This works ...