lintcode:Search Insert Position 搜索插入位置
题目:
搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
[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 搜索插入位置相关推荐
- [LeetCode] Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode - 35. Search Insert Position
35. Search Insert Position Problem's Link ---------------------------------------------------------- ...
- LeetCode算法入门- Search Insert Position -day19
LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...
- leetcode -- Search Insert Position
2019独角兽企业重金招聘Python工程师标准>>> Search Insert Position Given a sorted array and a target value, ...
- 【二分法】LeetCode 35. Search Insert Position
LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...
- 二分法:search insert position 插入位置
问题描述: 给定一个排序数组nums(无重复元素)与目标值target,如果target在nums里 出现,则返回target所在下标,如果target在nums里未出现,则返回target应该 插入 ...
- C#LeetCode刷题之#35-搜索插入位置(Search Insert Position)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3979 访问. 给定一个排序数组和一个目标值,在数组中找到目标值, ...
- LeetCode --Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- leetcode 【 Search Insert Position 】python 实现
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- Leetcode:Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
最新文章
- Solr和lucene
- Java中山脉的绘制---递归方法
- Java Review - 并发编程_LockSupport
- sqlbulkcopy是覆盖式更新吗_React 328道最全面试题(持续更新)
- C++ 类型转换(强制类型转换)
- makefile例子
- linux 远程备份mysql数据库_使用脚本自动化远程备份MySQL数据库
- HDU 5832——A water problem 2016CCPC网络赛1001
- LeetCode 1089. 复写零
- android加一减一控件,Android的步进器(增加/减少值)控件?
- 关于Breeze's MapHack 2.0的一些重要说明
- deliphi 字符串分割_delphi中拆分字符串的函数
- 现在的孩子为什么厌学的那么多?孩子厌学了怎么办?
- 还在繁琐的敲MVP接口和实现类吗,教你一秒搞定。
- 每日英语Daily English
- JS计算今天在本月第几周
- python 对excel的函数操作(2)
- 学习笔记19—dpabi错误集
- 软件项目管理复习要点
- 离线语音的自定义配置步骤
热门文章
- 编译OpenJDK8:get_msc_ver.sh:需要整数表达式/integer expression expected
- 解决办法:ubuntu登录后,桌面空空如也,状态栏没了
- 解决办法:对lzma_stream_decoder/lzma_code/lzma_end未定义的引用
- 插排与线分离设计的想法
- 进程标识符及fork
- 计算机三级网络技术路由,2009计算机三级网络技术:如何上网高效率宽带路由优化技巧放送...
- python 元编程有多强_马克的Python学习笔记#元编程 3
- GitHub、GitLab、Git的关系及开发
- nginx启动vue_Docker部署前端Vue
- cassandra可视化工具_精华 | 140种Python标准库、第三方库和外部工具都有了