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 ...
随机推荐
- Beaglebone Black–I2C 接 BMP280 获取当前温度
我有两个含温度传感的模块,一个是AOSONG 奥松电子的 AM2320 温度湿度,另一个是九轴里面的 Bosch BMP280.由于 AM2320 用 I2C MODBUS,直接用 I2C Tools ...
- 转 猫都能学会的Unity3D Shader入门指南(二)
猫都能学会的Unity3D Shader入门指南(二) 关于本系列 这是Unity3D Shader入门指南系列的第二篇,本系列面向的对象是新接触Shader开发的Unity3D使用者,因为我本身自己 ...
- c语言期末复习题
代码参考:<K&R> 1.单词计数 #include<stdio.h> #define IN 1 #define OUT 0 main() { int c, state ...
- 学习mongo系列(十一)关系
准备工作:首先在maxh数据库的address集合中先插入数据 > db.address.insert({child_address:"gansu"}) WriteResul ...
- 【Java基础】分支结构(1)
java 分支结构 if , if else , if elseif if /** 文件路径:G:\JavaByHands\if-else\ 文件名称:IfElseT.java 编写时间:2016/6 ...
- SAP的物料归档
我们在对前台对物料进行删除时,是物理删除,也就是打个删除标志,并没有正真的从数据库里删除,在前台还是可以看到的,下面介绍一下SAP的归档处理可以 把已删除的物料在前台删除掉,注意:项目里根据情况得到领 ...
- python核心编程第六章练习6-13
6-13.字符串.string模块包含三个函数,atoi(),atol()和atof(),他们分别负责把字符串转换成整型.长整型和浮点型数字.从Python 1.5起,Python的内建函数int() ...
- Centos 关闭后台进程 .sh 等
命令后加 & 符号可以让其在后台运行 如: node app.js & 想要关闭分两步: ps aux | grep app.js 查看app.js 所运行的进程号 kill 进程号 ...
- Web开发学习笔记
EnumUtils.isValidEnum(JnRouteProViewStateEnum.class, status) : 判断 status是否在 枚举集合中. MapUtils.isNotEmp ...
- 通过ksoap2-android来调用Web Service操作的实例
import java.io.IOException; import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObjec ...