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 ...
随机推荐
- Python 中的__new__和__init__的区别
[同] 二者均是Python面向对象语言中的函数,__new__比较少用,__init__则用的比较多. [异] __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例对象,是 ...
- Spring AOP之Introduction(@DeclareParents)简介
Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarati ...
- 【MyBatis】MyBatis之如何存储NULL
如果在sql语句中,不指定jdbcType,那么在存储中遇见null就会发生错误.一般情况下应该在每一个SQL语句的参数中指定jdbcType.比如: #{loginIp,jdbcType=VARCH ...
- JetBrains C++ IDE CLion配置与评测
等了大半年的JetBrains C++ IDE千呼万唤始出来!上次我猜2014年肯定发布,今天经@wet2_cn同学的提醒,我去官博一看,嘿!有了!赶紧安装试了一把,感觉这是迄今为止用过最好的Cpp ...
- android studio中使用git版本管理
转载请标注来源:http://blog.csdn.net/lsyz0021/article/details/51842774 AndroidStudio中使用Git-初级篇(一)——从github上传 ...
- Mac Apache Maven 配置
1.配置准备工作 1)配置 Maven 准备工作 下载相关软件 apache-maven-3.5.3.zip Maven 官网 Maven for Mac 配置软件下载地址,密码:q9u3. Mave ...
- 高级Unix命令
在Unix操作中有太多太多的命令,这些命令的强大之处就是一个命令只干一件事,并把这件事干好.Do one thing, do it well.这是unix的哲学.而且Unix首创的管道可以把这些命令任 ...
- RocketMQ os.sh 系统优化(CentOS)
贴出源码中的优化脚本先: #!/bin/sh # # Execute Only Once # echo 'vm.overcommit_memory=1' >> /etc/sysctl.co ...
- Java – Stream has already been operated upon or closed
Java – Stream has already been operated upon or closed package com.mkyong.java8; import java.util.Ar ...
- SecureCRT中使用 rz 上传文件 遇到 rz: command not found 的解决办法
-bash: rz: command not foundrz命令没找到?执行sz,同样也没找到. 安装lrzsz: # yum -y install lrzsz 现在就可以正常使用rz.sz命令上传. ...