LintCode Search Insert Position
找出指定target的位置(没有此数时为按顺序应当位置)。
public class Solution {
/**
* param A : an integer sorted array
* param target : an integer to be inserted
* return : an integer
*/
public int searchInsert(int[] A, int target) {
if(A == null) return -1;
if(A.length == 0) return 0;
int left = 0; int right = A.length - 1;
while(left + 1 < right){
int mid = (left + right) / 2;
if(A[mid] == target){
return mid;
}
else if(A[mid] < target){
left = mid;
}
else if(A[mid] > target){
right = mid;
}
}
if(A[left] >= target){
return left;
}
else if(A[right] < target){
return right + 1;
}
else return right;
}
}
LintCode Search Insert Position的更多相关文章
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- leetcode-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
随机推荐
- Adding List Item Element At Runtime In Oracle Forms
Add combo list / drop down list item element at runtime in Oracle forms.SyntaxPROCEDURE ADD_LIST_ELE ...
- 《BI那点儿事》数据流转换——数据转换
数据转换执行类似于T-SQL中的函数CONVERT或CAST的功能.数据转换的编辑界面如图,选择需要转换的列,在DataType下拉列表中选择需要的数据类型.Output Alias栏内设置输出时使用 ...
- RecyleView 简析
LayoutManager.Adapter是必须设置的(不设置将无法显示数据---不执行onCreateViewHolder()等方法) ItemDecoration\ItemAnimator是可选的 ...
- APP成功上线前的bug解决方案
首先测试用例设计阶段,设计并维护一个各个功能入口的说明文档.其实这个文档的作用很大,一方面对于bug回归阶段的人来说,这是用于提醒的;另外一个方面,在随机测试的时候,随机程度也能有所提高,测试人员能够 ...
- iOS开发UI篇—在UItableview中实现加载更多功能
一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据. 二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...
- JavaAppArguments
- 【lattice软核】MICO8流程
The LatticeMico System software is composed of three bundled applications: Mico System Builder (MS ...
- 【代码】verilog之:按键消抖
此模块完美运行 /*-------------------------------------------------------------------------------------- -- ...
- c# 配置文件之configSections配置(二)
在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的 System.Configuration.DictionarySectionHandler System. ...
- enmo_day_06
RAC Data Guard (DG) EMC NAS SAN 双活 数据完整性 约束 : 主键 : 非空 且 唯一 非空 : 唯一 : 外键 : 检查 : DISABLE, ENABLE VALID ...