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 ...
随机推荐
- h3c acl配置一列
acl number 3004 rule 0 permit ip source 10.2.1.4 0 rule 1 deny ip source 192.168.1.91 0 rule 2 deny ...
- UE4行为树
这是 UE4中行为树编辑器 中可用的默认节点.取决于开发项目的不同(如射击游戏),可能会有更多节点.这里介绍五种行为树节点类型: 节点类型 描述 Composite(流程控制节点) 这种节点定义一 ...
- mqtt mosquitto 源码安装
下载地址 ububtu : wget https://codeload.github.com/eclipse/mosquitto/zip/master 安装依赖 sudo apt-get insta ...
- 配置wildfly10为linux的服务,并开机启动
1.在opt路径下 下载 wildfly ,并解压下载下的压缩包 cd /opt sudo wget -c http://download.jboss.org/wildfly/10.0.0.Final ...
- EasyUI 学习(1)-Tooltip(提示框)
一.创建组件 Tooltip不依赖其他组件 1.使用class加载 <a href="#" class="easyui-tooltip" title=&q ...
- VMware安装xp虚拟机
VMware安装xp虚拟机 1.用到的软件: 2.安装VMware: 接受 选择自定义 要等上一小会. 输入密钥:百度一个就可以了. 安装成功: 禁用VMware网卡: 3.安装xp系统: 创建新的 ...
- 笔记本安装win10之后,无线网有问题
开机后,连接无线网络,过一段时间,就掉线了,然后再连接也连接不上,必须重启电脑才可以. 解决办法如下: 右键无线网络连接,状态,无线属性,安全,高级设置,勾上为此网络启用联邦信息标准兼容.
- [转] IPTables for KVM Host
IPTables for KVM Host January 26, 2012 By Andrew Galdes Use the following IPTables rules “/etc/sysco ...
- 探秘JS的异步单线程
对于通常的developer(特别是那些具备并行计算/多线程背景知识的developer)来讲,js的异步处理着实称得上诡异.而这个诡异从结果上讲,是由js的“单线程”这个特性所导致的. 我曾尝试用“ ...
- 组合拳出击-Self型XSS变废为宝
前言 作者:米斯特安全攻防实验室-Vulkey_Chen 博客:gh0st.cn 这是一个鸡肋性质的研究,也许有些标题党,请见谅- 本文启发于一些讨论,和自己脑子里冒出来的想法. 组合拳搭配 Self ...