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 ...
随机推荐
- 遍历HashMap的四种方法
public static void main(String[] args) { Map<String,String> map=new HashMap<String,String&g ...
- UTL_FILE详解
包UTL_FILE 提供了在操作系统层面上对文件系统中文件的读写功能.非超级用户在使用包UTL_FILE中任何函数或存储过程前必须由超级用户授予在这个包上的EXECUTE权限.例如:我们使用下列命令对 ...
- Java编程思想学习(十二) 数组和容器
一.数组 1).数组的多种初始化方式 下面总结了初始化数组的多种方式,以及如何对指向数组的引用赋值,使其指向另一个数组对象.值得注意的是:对象数组和普通数组的各种操作基本上都是一样的:要说有什么不同的 ...
- javascript判断文件大小
<input type="file" id="fileName" name ="fileName" onchange="Ge ...
- CSS3 动画animation
关键帧 什么是关键帧.一如上面对Flash原理的描述一样,我们知道动画其实由许多静态画面组成,第一个这样的静态画面可以表述为一帧.其中关键帧是在动画过程中体现了物理明显变化的那些帧. 比如之前的例子中 ...
- Mongodb for C# 分组查询
#region 排序获取集合 static List<BsonDocument> GetPagerWithGroup(string connectionString, string dat ...
- SQL Server 2005导入Excel表问题
EXCEL导入到SQL Server经常出现“文本被截断,或者一个或多个字符在目标代码页中没有匹配项” 原因: SQL Server的导入导出为了确定数据表的字段类型,取excel文件的前8行来判别. ...
- java分布式通信系统(J2EE分布式服务器架构)
一.序言 近几个月一直从事一个分布式异步通信系统,今天就整理并blog一下. 这是一个全国性的通信平台,对性能,海量数据,容错性以及扩展性有非常高的要求,所以在系统的架构上就不能简单的采用集中式.简单 ...
- ubuntu安装wiz笔记
wiz笔记支持跨平台 下面记录一下如何在ubuntu下面安装wiz笔记 1,ubuntu默认是没有wiz资源的,需要先添加官方ppa软件仓库 sudo add-apt-repository ppa:w ...
- 繁华模拟赛 Vicent坐电梯
/*n<=5000这样就不能用O(n)的转移了,而是要用O(1)的转移.注意我们每次的转移都来自一个连续的区间,而且我们是求和区间求和?前缀和!令sum[step][i]表示f[ste ...