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 ...
随机推荐
- 软件公司的两种管理方式 总体来说,这个世界上存在两种不同的软件公司的组织结构。我把他们叫做 Widget Factory(小商品工厂) 和 Film Crews(电影工作组
软件公司的两种管理方式 一个简单的回答应该是——“因为在我们的社会里,我们总是会认为薪水和会和职位的层次绑在一起”.但是,这个答案同时也折射出一个事实——我们的薪资是基于我们的所理解的价值,但这并没有 ...
- POJ 1637 Sightseeing tour (SAP | Dinic 混合欧拉图的判断)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6448 Accepted: 2654 ...
- struct2 拦截所有没有登录的用户,强行转到登录界面AuthorizationInterceptor
package com.sise.action; import java.util.Map; import com.opensymphony.xwork2.Action; import com.ope ...
- golang学习笔记 --switch
switch的例子: switch coinflip() { case "heads": heads++ case "tails": tails++ defau ...
- 验证时出错。HRESULT = '8000000A'
这本来是在VS2005下创建的一下项目,后来改用VS2010的开发环境,.NET Framework的版本号还是使用2.0, 但每次生成之后都会在解决方式的同级文件夹下产生一个名称乱码的文件夹, 攻克 ...
- PMD-Java 代码检查工具对 error 和 warning 的配置
PMD是一款优秀的Java程序代码检查工具.该它可以检查Java代码中是否含有未使用的变量.是否含有空的抓取块.是否含有不必要的对象等. 但在使用过程中,你会项目中发现存在大量的 PMD 插件报出的 ...
- chrome 开发并使用油猴 Tampermonkey 插件
背景:以前 test.user.js 的插件方式被 Chrome 封杀了.现在只能依赖油猴来编写自己的 js 插件. 官方网站:https://tampermonkey.net/ chrome商店: ...
- 正则和xpath在网页中匹配字段的效率比较
1. 测试页面是 https://www.hao123.com/,这个是百度的导航 2. 为了避免网络请求带来的差异,我们把网页下载下来,命名为html,不粘贴其代码. 3.测试办法: 我们在页面中 ...
- rocketMq排坑:如何设置rocketMq broker的ip地址
在工作中遇到了一个这个问题,就是我们rocketmq是部署在云主机上的 但是我们的开发同事在自己的电脑连接rocketmq链接不上 报错显示Caused by: org.apache.rocketmq ...
- css冻结列的效果
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8&quo ...