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 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 搜索插入位置的更多相关文章
- [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 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 035 Search Insert Position 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...
- 【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- [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 ...
- 【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 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Search insert position, 查找插入位置
问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...
- LintCode Search Insert Position
找出指定target的位置(没有此数时为按顺序应当位置). public class Solution { /** * param A : an integer sorted array * para ...
随机推荐
- struct和class区别
转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...
- js实现checkbox的全选/取消
所有的操作都将使用jquery进行. 主要是为了实现指定内容的批量/单独删除操作. 先看一下页面的设计. 实现操作的主要地方是: 首先实现单击“标题”旁的checkbox实现所有条目的选择. 要点:j ...
- PHP中数组排序实例学习
先介绍下php中用于数组排序的函数: 排序方法 升序 降序 ...
- Winform Krypton控件使用(一)
在学生健康系统中前期考虑需求中,考虑过在C/S下使用Winform或WPF完成项目, 在winform下,考虑过两套插件,一个是DotNetBar, 控件很多,但这个是收费的,考虑到以后的版权和费用问 ...
- 隐藏wmware到系统托盘
[此方法是百度到的,经整理放在这里以防忘记.] 1.打开VMware Authorization Service服务.控制面板--管理工具--服务,在里面找到VMware Authorization ...
- SQLdiag Utility
使用SQLdiag 会进行信息搜集类型 Windows 系统性能日志 Windows 系统日志 SQL Server 性能信息 SQL Server 阻塞信息 SQL Server 配置信息 如何使用 ...
- ORA-00845
系统版本: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 数据库版本: Oracle Database ...
- 10、WPF程序集
WPF核心程序集 PresentationCore.dll:这个程序集定义了许多构成WPF GUI层基础的类型.例如包含WPF Ink API(pc笔针输入,手写输入)的支持.几个动画基元以及几个图形 ...
- 实用项目管理前台框架:EasyUI,ExtJs
EasyUI项目管理框架,如图: 项目名称:微信管理平台 项目地址:http://www.cnblogs.com/hanyinglong/p/3236966.html#3007557 托管地址:htt ...
- android重写view和viewgroup的区别
重写view: View类一般用于绘图操作,重写它的onDraw方法,但它不可以包含其他组件,没有addView(View view)方法. 重写viewgroup: ViewGroup是一个组件容器 ...