Java for LeetCode 035 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
解题思路一:
二分查找,注意边界条件,JAVA实现如下:
static public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
if (target == nums[(left + right) / 2])
return (left + right) / 2;
else if (target < nums[(left + right) / 2]) {
if ((left + right) / 2 - 1 < left)
return target > nums[left] ? left + 1 : left;
if (target > nums[(left + right) / 2 - 1])
return (left + right) / 2;
if (target == nums[(left + right) / 2 - 1])
return (left + right) / 2 - 1;
right = (left + right) / 2 - 1;
} else {
if ((left + right) / 2 + 1 > right)
return target > nums[right] ? right + 1 : right;
if (target <= nums[(left + right) / 2 + 1])
return (left + right) / 2 + 1;
left = (left + right) / 2 + 1;
}
}
return right;
}
解题思路二:
同样采用二分,可以通过某些方法,减少代码量,JAVA实现如下:
static public int searchInsert(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left < right) {
if (nums[(right + left) / 2] < target)
left = (right + left) / 2 + 1;
else
right = (right + left) / 2;
}
return nums[left] >= target ? left : left + 1;
}
Java for LeetCode 035 Search Insert Position的更多相关文章
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Java [leetcode 35]Search Insert Position
题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- LeetCode 35. Search Insert Position (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- 035 Search Insert Position 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
随机推荐
- Android系统中的广播(Broadcast)机制简要介绍和学习计划
在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制:这些组件甚至是可以位于不同的进程中,这样它就像Binder机制一样,起到进程间通信的作用:本文通过一个 ...
- jquery中datagrid中getSelected和getSelections的应用
http://blog.sina.com.cn/s/blog_8e50ede90101fff9.html 刚开始使用jquery的datagrid就知道如果要对特定的一行进行编辑,可以是 $('#on ...
- yii框架常用url地址
调用YII框架中 jquery:Yii::app()->clientScript->registerCoreScript('jquery'); framework/web/j ...
- 一排cell就第一个cell要点两次才响应,其他的cell都点一下就响应
一开始还以为是控件的问题,后来查了下百度,没办法谷歌不能用,结果有人说方法写错了 -(void)tableView:(UITableView *)tableView didSelectRowAtInd ...
- 有关基于模型的设计(MBD)一些概念和理解(zz)
http://www.matlabsky.com/thread-38774-1-1.html 本文转载于MathWorks中国高级工程师董淑成的帖子内容.为了方便阅读,对原文进行了重新整理编辑. 之前 ...
- LINQ to SQL更新数据库操作(转载)
使用LINQ to SQL建模Northwind数据库 在这之前一起学过LINQ to SQL设计器的使用,下面就使用如下的数据模型: 当使用LINQ to SQL设计器设计以上定义的五个类(Prod ...
- Mongodb For C# "Query" 对象常用的方法
Query.All("name", "a", "b");//通过多个元素来匹配数组 Query.In("name", & ...
- 无法卸载windows组件?提示zClientm.exe
在卸载windows的组件: wmp, outlook, msessager等,提示: 找不到zClientm.exe 文件? 原来, zclientm.exe是windows的游戏组件依赖的文件, ...
- git曲线
git 更新牵涉到三个地方的内容: ?? (pull下来的) 正在编辑的目录, 即工作空间; 本地的仓库; (这个动作叫 : commit) github.com上的服务器即 refs (这个动作 ...
- Gulp, 比Grunt更好用的前端构建工具
Gulp, 比Grunt更好用的前端构建工具 本文主要从两个方面介绍Gulp:一,Gulp相对于Grunt的优势: 二,Gulp的安装和使用流程 Gulp相对于Grunt的优势 gulp.js 的作者 ...