LeetCode: Search Insert Position 解题报告
Search Insert Position
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
SOLUTION 1:
用九章算法的模板查找结束后判断一下即可。:
public int searchInsert1(int[] A, int target) {
if (A == null || A.length == 0) {
return 0;
}
int left = 0;
int right = A.length - 1;
while (left < right - 1) {
int mid = left + (right - left) / 2;
int num = A[mid];
if (num == target) {
return mid;
} else if (num < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// bug 1: should use <=
if (target <= A[left]) {
return left;
// bug 2: should use <= . consider that may the result exit in left or right.
} else if (target <= A[right]) {
return right;
}
return right + 1;
}
SOLUTION 2:
也可以很简洁:
http://fisherlei.blogspot.com/2013/01/leetcode-search-insert-position.html
http://blog.csdn.net/fightforyourdream/article/details/14216321
这样可以word的原因是:
1. 当target存在,当然返回mid.
2. 当target大于所有的数。则l, r会跑到最右边,并且l会继续跑出去一格,也就是l会指向 len,也就是要找的值。
3. 当target小于所有的数。l,r跑到最左边,并且r会继续往左移动一格,l指向目标位置。
4. 当target小于某数a,并大于某数b。那么l, r中止时,r会在b,l 会在a,l 指向目标位置。
若是找不到target, 循环结束后l 的值是 与target最接近但是 > target 的数在数组中的位置。
// sol 2:
public int searchInsert(int[] A, int target) {
if (A == null || A.length == 0) {
return 0;
} int left = 0;
int right = A.length - 1; while (left <= right) {
int mid = left + (right - left) / 2;
int num = A[mid]; if (num == target) {
return mid;
} else if (num < target) {
left = mid + 1;
} else {
right = mid - 1;
}
} return left;
}
GTIHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/divide2/SearchInsert.java
LeetCode: Search Insert Position 解题报告的更多相关文章
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- 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 搜索插入位置
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 ...
随机推荐
- 树莓派进阶之路 (008) - 树莓派安装ftp服务器(转)
vsftpd是开源的轻量级的常用ftp服务器. 1,安装vsftpd服务器 (约400KB) sudo apt-get install vsftpd 2,启动ftp服务 sudo serv ...
- 【转】细说UI线程和Windows消息队列
在Windows应用程序中,窗体是由一种称为“UI线程(User Interface Thread)”的特殊类型的线程创建的. 首先,UI线程是一种“线程”,所以它具有一个线程应该具有的所有特征,比如 ...
- MYSQL IN 与 EXISTS 的优化示例介绍
优化原则:小表驱动大表,即小的数据集驱动大的数据集. ############# 原理 (RBO) ##################### select * from A where id in ...
- C语言中 不定义结构体变量求成员大小
所谓的求成员大小, 是求成员在该结构体中 用 sizeof(结构体名.结构体成员名) 求来的. 很多时候我们需要知道一个结构体成员中的某个成员的大小, 但是我们又不需要定义该结构体类型的变量(定义的话 ...
- 适合移动端与PC端的 CSS Reset - m_base.css
文章来源:http://www.cnblogs.com/HCJJ/p/6399185.html 具体代码 @charset "utf-8"; html { -ms-text-siz ...
- SQL中实现SPLIT函数几种方法
例1 代码如下 复制代码 create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))returns @temp ...
- 【Redis源代码剖析】 - Redis内置数据结构之字典dict
原创作品,转载请标明:http://blog.csdn.net/Xiejingfa/article/details/51018337 今天我们来讲讲Redis中的哈希表. 哈希表在C++中相应的是ma ...
- Redis使用问题及知识点记录 - 待整理
介绍 官网:https://redis.io/commands/expire spring data redis 整合redis使用方法 spring 整合api :http://docs.sprin ...
- Vue 动态组件、动画、插件
1 动态组件 ①简单来说: 就是几个组件放在一个挂载点下,然后根据父组件的某个变量来决定显示哪个,或者都不显示. ②动态切换: 在挂载点使用component标签,然后使用v-bind:is=”组件名 ...
- unity, Collider2D.bounds的一个坑
Note that this will be an empty bounding box if the collider is disabled or the game object is inact ...