leetcode — search-insert-position
/**
* Source : https://oj.leetcode.com/problems/search-insert-position/
*
* Created by lverpeng on 2017/7/14.
*
* Given a sorted array and a target value, return the index if the target is found.
* If not, return the index where it would be if it were inserted in order.
*
* You may assume no duplicates in the array.
*
* Here are few examples.
* [1,3,5,6], 5 → 2
* [1,3,5,6], 2 → 1
* [1,3,5,6], 7 → 4
* [1,3,5,6], 0 → 0
*
*/
public class SearchInsertPosition {
/**
* 二分查找
*
* @param num
* @param target
* @return
*/
public int binarySearch (int[] num, int target) {
int left = 0;
int right = num.length - 1;
int mid = 0;
while (left <= right) {
mid = (left + right) / 2;
if (num[mid] == target) {
return mid;
}
if (target > num[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
public static void main(String[] args) {
SearchInsertPosition searchInsertPosition = new SearchInsertPosition();
int[] arr = new int[]{1,3,5,6};
System.out.println(searchInsertPosition.binarySearch(arr, 5));
System.out.println(searchInsertPosition.binarySearch(arr, 2));
System.out.println(searchInsertPosition.binarySearch(arr, 7));
System.out.println(searchInsertPosition.binarySearch(arr, 0));
}
}
leetcode — 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: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [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 ...
- [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 t ...
- LeetCode Search Insert Position (二分查找)
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode——Search Insert Position
Description: Given a sorted array and a target value, return the index if the target is found. If no ...
- [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 (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
随机推荐
- shell脚本编写某一文件夹内拷贝某一段文件(有则跳过没有则拷贝)
必须是同一台服务器下,或者挂载目录,不同服务器下没办法查询目录中是否有该文件 如果不在同一服务器下,可以把要查询的那个服务器的文件夹设置共享挂在到当前服务器 或者可以把脚本写到要拷贝的服务器上,那么s ...
- cpu的工作原理
- 手机号读取城市数据库2018年3月excel版
EXCEL表中是更新到2018年3月份的手机归属地数据库. 手机号读取城市数据库2018年3月excel版.zip
- IOS 视频.图片上传服务器
//上传视频 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer. ...
- Day05 (黑客成长日记) 文件操作系列
文件操作: 1.以什么编码方式输出,就以什么编码方式打开 f = open('d:\文件操作.txt',mode='r',encoding='GB2312') G = f.read() print(G ...
- webstorm验证码
2017-2-16 亲测可用 http://idea.imsxm.com/ webstorm10 注册码 User or company Name: EMBRACE ===== LICENSE KEY ...
- php中连接mysql数据库的第一步操作
<?phperror_reporting(E_ALL ^ E_DEPRECATED);//设置报警级别人$mylink = mysql_connect("localhost" ...
- python基础自学 第一天
python的概述 python的创始人:吉多·范罗苏姆 1991年,第一个python解释器诞生,用C语言实现,并能调用C语音的库文件 解释器(解释型语言和编 译型语言的区别) 把其他语言翻译成计算 ...
- JavaScript自定义鼠标右键菜单
下面为JavaScript代码 window.onload = function () { //好友列表 var f = 0; //判断指定id的元素在页面中是否存在 if (document.get ...
- 背水一战 Windows 10 (110) - 通知(Tile): secondary tile 模板之基础, secondary tile 模板之文本
[源码下载] 背水一战 Windows 10 (110) - 通知(Tile): secondary tile 模板之基础, secondary tile 模板之文本 作者:webabcd 介绍背水一 ...