[LeetCode][Java] 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
题意:
给定一个有序数组和一个目标值。假设在数组中找到该目标值。就返回这个目标值的位置标号。假设没有找到,就返回该目标值插入该数组中时的位置标号。
你能够如果该数组中没有反复元素。
以下是一些列子:
[1,3,5,6]
,
5 → 2
[1,3,5,6]
,
2 → 1
[1,3,5,6]
,
7 → 4
[1,3,5,6]
,
0 → 0
算法分析:
二分搜索就可以。
直接上代码
AC代码:
<span style="font-size:12px;">public class Solution
{
public int searchInsert(int[] A, int target)
{
int i = 0;
int j = A.length - 1; while (i <= j)
{
int mid = (int)((i + j)/2);
if (A[mid] == target)
return mid;
else if (A[mid] > target)
j = mid - 1;
else
i = mid + 1;
}
return i;
}
}</span>
[LeetCode][Java] Search Insert Position的更多相关文章
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [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 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 ...
- 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 在给定的有序数组中插入一个目标数字,求出插入 ...
- [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 ...
随机推荐
- Node FS 读取文件中文乱码解决
1:首先保证源文件编码方式为UTF-8 2:读取代码,设置编码方式rs.setEncoding('utf8') var fs = require('fs'); var rs = fs.createRe ...
- Android ViewPager用法小结
android-support-v4.jar 是谷歌提供给我们的一个兼容低版本号安卓设备的软件包.里面包囊了仅仅有在 Android 3.0 以上可用的API.而 ViewPager 就是当中之中的一 ...
- java面试第四天
修饰符static: 把对象相关的变成类相关的,它可以修饰属性.方法.代码块和内部类 static修饰属性(类变量): 那么这个属性就可以用" 类名.属性名 "来访问,也就是使这个 ...
- maven POM.xml内的标签大全详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Linux密钥认证错误解决
问题描述: Xshell用key认证登录,提示所选的用户密钥未在远程主机上注册 问题解决: 查看日志/var/log/secure,基本上都是用户根目录的权限问题 根据日志提示: Authentica ...
- tomcat在线部署且查看堆栈状态
配合ab压测tomcat站点的并发量,适当调整JVM参数,堆栈,连接数 00.修改conf/tomcat-user.xml 1. 在$Tomcat_Home/conf/tomcat-users.xml ...
- 【J2EE之web应用】java集群概念
在学习web应用进行部署的时候,遇到一个名词java集群,(事实上遇到非常多名词╭(╯^╰)╮~~~).不懂意思就查一查! 在这里做个笔记! 没有什么高深见解,就搞明确几个概念,java集群的特点 . ...
- 【laravel5.4】{{$name}}、{{name}}、@{{$name}} 和 @{{name}} 的区别
1.前面带@符号的,表示不需要laravel的blade引擎进行解析:有@的,则需要blade解析 2.{{$name}} 表示:blade解析 后台php的 name变量 {{name}} 表示:b ...
- HDUOJ---1863畅通工程
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- html5中的FileReader对象
表单中有图片选项,选中图片文件之后要求可以预览.这个功能很多控件都封装好了,但是它们的底层都是FileReader对象. FileReader对象提供了丰富的功能,包括以二进制.以文本方式读取文件内容 ...