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 ...
随机推荐
- 使用rar把程序打包成一个exe
根目录--全部文件--右键添加到压缩文件 常规--创建自解压压缩文件 高级--自解压选项 解压路径--Finger(自己写)--在"Program Files"中创建 设置--解压 ...
- Linux环境(Centos7)下部署.NetCore2.0的Web应用
Web应用基于Windows环境下开发,然后部署到Linux 1.进入VS2017,点击新建->项目->.NetCore->ASP.NET Core Web应用程序,确定 2.选择W ...
- (24)How generational stereotypes hold us back at work
https://www.ted.com/talks/leah_georges_how_generational_stereotypes_hold_us_back_at_work/transcript ...
- Linux编程之fork函数
在Linux中,fork函数的功能就是在一个进程中创建一个新的进程,当前调用fork函数的进程就是产生的新进程的父进程,新进程在以下也称为子进程.在新进程生成之后就会在系统中开始执行. 函数原型:pi ...
- svn提交出现错误 svn: Working copy 'D:\...'locked.
更新svn内容时出现如下的错误: svn: Working copy 'D:\tools\Workspaces\EclipseForNewSTLJ\javashop\b2c\src\main\weba ...
- Android程序backtrace分析方法
如何分析Android程序的backtrace 最近碰到Android apk crash的问题,单从log很难定位.从tombstone里面得到下面的backtrace. *** *** *** * ...
- EF学习笔记(八):更新关联数据
学习笔记主目录链接:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 上一篇链接:EF学习笔记(七):读取关联数据 本篇原文链接:Updating Related Data 本篇主要考 ...
- position 小结
position: static fixed relative absolute sticky 1.static static定位是HTML元素的默认值,即没有定位,元素出现在正常的流中.因此,这种定 ...
- this用法总结
在JavaScript中,this关键字可以说是最复杂的机制之一.对this的作用机制缺乏比较深入的理解很容易在实际开发中出现问题. 1.this的作用 为什么要在JavaScript中使用this呢 ...
- SSD硬盘安装win10 且安装千牛工作台频繁卡死问题解决过程
之前win7的时候突然出现卡死现象,具体表现为:磁盘占用百分之百,千牛窗口无法关闭,点截图按钮后,千牛关闭了,并没有截图,千牛聊天输入/无法出现快捷短语了,电脑桌面点击右键没反应,任务栏点击右键也没反 ...