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 ...
随机推荐
- MATLAB 不能保存变量问题及解决办法
在使用matlab保存结构体.元胞数组等等的变量时,matlab总是提示 警告: 未保存变量 'session'.对于大于 2GB 的变量,请使用 MAT 文件版本 7.3 或更高版本. 问题如下: ...
- java struts2入门学习---中文验证、对错误消息的分离、结果(result)类型细节配置
一.需求 利用struts2实现中文验证并对错误消息的抽离. 详细需求:用户登录-->不填写用户名-->页面跳转到用户登录页面,提示用户名必填(以英文和中文两种方式提示)-->填写英 ...
- 在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
原文 http://www.jb51.net/article/75671.htm 图片剪切一.使用Quartz2D完成图片剪切1.把图片显示在自定义的view中先把图片绘制到view上.按照原始大小 ...
- 【struts2】Struts2的异常处理
在Action中execute方法声明为:public String execute() throws Exception,这样,Action可以抛出任何Exception. 1)自己实现异常处理 我 ...
- 【Linux】字符转换命令col
[root@www ~]# col [-xb] 选项与参数: -x :将 tab 键转换成对等的空格键 -b :过滤掉所有的控制字符,包括RLF(Reverse Line Feed)和HRF(Halt ...
- 你永远学不会的设计-->百度地图map api key 的设置
哈哈 说到咱的互联网大佬 baidu, 真是无人不知,无人不晓,可是多少人能领会他的设计哲学呢? 来看看吧.... 事情要从上周的一个小实验说起,目标是调用baidu map api 做个产品原型. ...
- SNF.Net 快速开发平台Spring.Net.Framework 诞生的由来与规划
没有快速开发平台的时候只能感慨自己曾经浪费了那么多精力在拖拽控件上,总写重复的代码,却花了很多精力且不能体现自己的价值.SNF快速开发平台能把你解放出来,让你有更多的时间参与到核心业务逻辑中去,让你有 ...
- 解决ssh连接慢(有时候等半分钟才出现密码输入提示)的方法
经常通过ssh 或者 scp 连接一堆远程主机,同样是 Linux 主机,其中一些创建 ssh 连接速度特别慢,连接建立之后执行操作速度却很正常,看来应该不是网络原因.解决的方法是通过ssh 的-v参 ...
- 类里的通用成员函数应声明为static
类C的成员函数f,如果f的实现实现不依赖于C的任何成员变量,则f为通用函数. 对于通用函数f,可以将其从类C中分离出来做成一个全局函数,也可以仍然让它属于类C,但加上static. 两种处理方法实际都 ...
- elasticsearch 性能监控基础
一.Elasticsearch 是什么 Elasticsearch是一款用Java编写的开源分布式文档存储和搜索引擎,可以用于near real-time存储和数据检索. 1.Elasticsearc ...