/**
* 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的更多相关文章

  1. 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 ...

  2. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. [LeetCode] Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. [LeetCode] Search Insert Position 二分搜索

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. [LeetCode] Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. 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 ...

  7. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  8. LeetCode——Search Insert Position

    Description: Given a sorted array and a target value, return the index if the target is found. If no ...

  9. [Leetcode] search insert position 寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  10. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

随机推荐

  1. IntelliJ IDEA 控制台中文乱码解决方案

    配置Intellij的配置文件(在idea安装目录bin目录下) 打开Intellij的根目录,找到下图的两个文件(根据你的系统是32位或64位选择其中一个配置文件),在配置文件中添加: -Dfile ...

  2. 每日一练ACM 2019.04.13

    2019.04.13 第1002题:A+B Proble Ⅱ Problem DescriptionI have a very simple problem for you. Given two in ...

  3. Redis讲解以及测试要点

    一.redis 简介 redis适合放一些频繁使用,比较热的数据,因为是放在内存中,读写速度都非常快,一般会应用在下面一些场景,排行榜.计数器.消息队列推送.好友关注.粉丝. 首先要知道mysql存储 ...

  4. aircrakf

    airmon-ng start wlan0 airodump-ng wlan0mon#find the wifi airodump-ng -w yakoazz -c 1 --bssid BE:5F:F ...

  5. Unity3D中默认函数的执行顺序

    直接用一张图来说明各个默认函数的执行顺序: FixedUpdate以固定的物理时间间隔被调用,不受游戏帧率影响.一个游戏帧可能会调用多次FixedUpdate.比如处理Rigidbody的时候最好用F ...

  6. js实现全屏和缩放

    /** * @description 简单的浏览器检查结果. * `webkit` * webkit版本号,如果浏览器为非webkit内核,此属性为`undefined`. * `chrome` * ...

  7. spring 5.1.2 mvc RequestMappingHandlerMapping 源码初始化过程

    RequestMappingHandlerMapping getMappingForMethod RequestMappingHandlerMapping 继承于 AbstractHandlerMet ...

  8. ES6学习:两个面试题目--关于模板字符串

    号称看完就能“让开发飞起来”,不过文中的两个面试题目的知识点并没包括在文中. https://www.jianshu.com/p/287e0bb867ae 文中并没有完整的知识点去完成上面的两道题,这 ...

  9. Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

  10. Charles 学习笔记

    之前接触过抓包工具Fiddler,一直在Windows下使用,感觉还是挺好用的.今天接触了Charles才有了了解,首先Charles设计的比较美丽,而且页面布局也是简单易学的,但这两款抓包工具本质的 ...