题目:

搜索插入位置

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

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

样例

[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 hereif(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;}
}

View Code

总耗时: 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 hereif nums==None:return 0if len(nums)==0:return 0start = 0end = len(nums)-1 while start<=end:median = (start + end)/2if nums[median] == target:return medianelif nums[median] < target:start = median + 1else:end = median - 1return start

View Code

总耗时: 474 ms

转载于:https://www.cnblogs.com/theskulls/p/4883876.html

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

    35. Search Insert Position Problem's Link ---------------------------------------------------------- ...

  3. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  4. leetcode -- Search Insert Position

    2019独角兽企业重金招聘Python工程师标准>>> Search Insert Position Given a sorted array and a target value, ...

  5. 【二分法】LeetCode 35. Search Insert Position

    LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...

  6. 二分法:search insert position 插入位置

    问题描述: 给定一个排序数组nums(无重复元素)与目标值target,如果target在nums里 出现,则返回target所在下标,如果target在nums里未出现,则返回target应该 插入 ...

  7. C#LeetCode刷题之#35-搜索插入位置(Search Insert Position)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3979 访问. 给定一个排序数组和一个目标值,在数组中找到目标值, ...

  8. LeetCode --Search Insert Position

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

  9. leetcode 【 Search Insert Position 】python 实现

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

  10. Leetcode:Search Insert Position

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

最新文章

  1. Solr和lucene
  2. Java中山脉的绘制---递归方法
  3. Java Review - 并发编程_LockSupport
  4. sqlbulkcopy是覆盖式更新吗_React 328道最全面试题(持续更新)
  5. C++ 类型转换(强制类型转换)
  6. makefile例子
  7. linux 远程备份mysql数据库_使用脚本自动化远程备份MySQL数据库
  8. HDU 5832——A water problem 2016CCPC网络赛1001
  9. LeetCode 1089. 复写零
  10. android加一减一控件,Android的步进器(增加/减少值)控件?
  11. 关于Breeze's MapHack 2.0的一些重要说明
  12. deliphi 字符串分割_delphi中拆分字符串的函数
  13. 现在的孩子为什么厌学的那么多?孩子厌学了怎么办?
  14. 还在繁琐的敲MVP接口和实现类吗,教你一秒搞定。
  15. 每日英语Daily English
  16. JS计算今天在本月第几周
  17. python 对excel的函数操作(2)
  18. 学习笔记19—dpabi错误集
  19. 软件项目管理复习要点
  20. 离线语音的自定义配置步骤

热门文章

  1. 编译OpenJDK8:get_msc_ver.sh:需要整数表达式/integer expression expected
  2. 解决办法:ubuntu登录后,桌面空空如也,状态栏没了
  3. 解决办法:对lzma_stream_decoder/lzma_code/lzma_end未定义的引用
  4. 插排与线分离设计的想法
  5. 进程标识符及fork
  6. 计算机三级网络技术路由,2009计算机三级网络技术:如何上网高效率宽带路由优化技巧放送...
  7. python 元编程有多强_马克的Python学习笔记#元编程 3
  8. GitHub、GitLab、Git的关系及开发
  9. nginx启动vue_Docker部署前端Vue
  10. cassandra可视化工具_精华 | 140种Python标准库、第三方库和外部工具都有了