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 ...
随机推荐
- ruby 资源收集
http://huacnlee.com/blog/archives/ 一个牛人李华顺的个人网站 http://api.rubyonrails.org/ 不错的ruby on rails学习网站 htt ...
- mac 显示set a breakpoint in malloc_error_break to debug 终端显示进程已完成怎么办?
mac 终端显示 ,0x7fff73dbd300) malloc: *** error for object 0x7fce52d15100: pointer being freed was not a ...
- Oracle的控制文件
一.控制文件 oracle的控制文件是极其重要的文件,它是一个较小的二进制文件. 记录了当前数据库的结构信息,同时也包含数据文件及日志文件的信息以及相关的状态,归档信息等等 在参数文件中描述其位置, ...
- K-邻近算法
K-邻近算法 采用测量不同特征值之间的距离来进行分类 Ad:精度高,对异常值不敏感,无数据输入假定 Na:计算复杂度高,空间复杂度高 KNN原理 存在样本集,每个数据都存在标签,输入无标签的新数据后, ...
- Eclipse 最全快捷键
英文的
- ubuntu忘记密码怎么办
刚安装了,ubuntu14.04,就想着,如果忘记登录密码,这可不好办,所以测试下开机,刚过bios显示画面,不停的点击,,键盘左边的shift键.(因为刚开始是采用按着不放的办法,结果不灵.所以我不 ...
- python中requests
#发送无参数的get请求import requests def get_html(url): res = requests.get(url) return res.text #发送无参数的post请求 ...
- CSS权重及样式优先级问题
CSS权重值计算 一条样式规则的整体权重值包含四个独立的部分:[A, B, C, D]; (1) A 表示内联样式(写在标签的style属性中),只有 1 或者 0 两个值:对于内联样式,由于没有选择 ...
- 学习PYTHON之路, DAY 2 - PYTHON 基础 2(基础数据类型)
一 字符串格式化输出 name = 'nikita' age = 18 print ("I'am %s, age is %d") % (name, age) PS: 字符串是 %s ...
- 20135203齐岳信息安全系统设计基础——实验四&实验五实验报告
见20135217孙小博的博客:http://www.cnblogs.com/sunxiaobo/p/4991861.html