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 ...
随机推荐
- gson的特殊用法
1.gson包在处理 字符串转 Map 或者 List 的方法. List memberList = gson.fromJson(str,new TypeToken<List>() {}. ...
- python模块:logging
# Copyright 2001-2016 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and ...
- Java 包与类的命名(util、service、tool、dao )区别
util 通用的.与业务无关的,可以独立出来,可供其他项目使用.方法通常是public static,一般无类的属性,如果有,也是public static. service 与某一个业务有关,不是通 ...
- 二叉树遍历之三(Moriis traversal)
二叉树的Morris traversal是个很值得学习的算法,也是此系列重点想要记叙的一个算法.Morris traversal的一个亮点在于它是O(1)空间复杂度的.前面的递归和迭代都是需要O(n ...
- angular-指令
ng-app 作用域 ng-init 声明 module 模块 ng-model 双向绑定 ng-bind 绑定 angular是一个MVC框架:即 M------------------module ...
- IO在Socket中的应用
一.BIO 在JDK1.4出来之前,我们建立网络连接的时候采用BIO模式,需要先在服务端启动一个ServerSocket,然后在客户端启动Socket来对服务端进行通信,默认情况下服务端需要对每个连接 ...
- spring 框架学习网站
spring 框架学习网站 NO1 http://www.mkyong.com NO2 https://spring.io/docs/reference
- email program (客户端)演变过程有感
以下内容全部为个人读后感(参考百度百科的相关资料) 首先我认为电子邮件是一个非常伟大的发明,它不仅成本低,而且传输效率快! 关于它的起源,我从百度百科中看到了两种说法 1.1969年10月世界 ...
- 原来你离BAT只有一步之遥
ladies and乡亲们 喜迎全民嗨购双11 i春秋准备搞一波大优惠 优惠力度有多大 跨店凑单满400-50? 指定商品199减100? 史无钜惠 不凑单 不指定 一次直降9000元 原价:2580 ...
- Android 视频播放器 (三):使用NBPlayer播放直播视频
一.前言 在 Android 音视频开发学习思路 中,我们不断的学习和了解音视频相关的知识,随着知识点不断的学习,我们现在应该做的事情,就是将知识点不断的串联起来.这样才能得到更深层次的领悟.通过整理 ...