[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 ...
随机推荐
- AVL树的实现例程
/* AVL树的节点声明 */ #ifndef _AVLTREE_H #define _AVLTREE_H struct AvlNode; typedef struct AvlNode *Positi ...
- jdbc第二天
事务 l 连接池 l ThreadLocal l BaseServlet自定义Servlet父类(只要求会用,不要求会写) l DBUtils à commons-dbutils 事务 l 事务的四大 ...
- 启动mysql出现1067错误
0. 打开mysql\bin\my.ini,查找[mysqld],在[mysqld]下面添加一行文字,skip-grant-tables 即组成 [mysqld] skip-grant-tables[ ...
- nexus 离线更新索引
1.到http://repo.maven.apache.org/maven2/.index/页面下载下面这两个文件: nexus-maven-repository-index.gz nexus-mav ...
- Linux下设置和查看环境变量(转)
Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1 永久的:需要修改配置文件,变量永久生效. 2 临时的:使用export命令声明即可,变量在关闭shell时失效. 设置变量 ...
- Oracle 去重查询
Oracle 去重查询 CreateTime--2018年2月28日15:38:45 Author:Marydon (一)使用distinct --查询指定区间内表停诊字段的值 SELECT DI ...
- java 中文转拼音之pinyin4j
一.简介 有时候,须要将汉字编程相应的拼音.以方便数据的处理.比方在Android手机应用的开发上.要查询联系人的姓名.通常都是用拼音进行查询的. 比方要查询"曹孟德",就能够输入 ...
- 域名无法解析 Linux临时或永久修改DNS
最近给VPS重装了系统,因为服务商不提供DHCP,所以只好手动设置IP和DNS Server.悲催的是系统重装的时候忘记了输入DNS Server,最后导致进去系统后,各种域名无法解析. Linux中 ...
- Ubuntu共享WiFi(AP)给Android方法
更新: 2012-03-03 Android是不支持Ad-hoc模式的WiFi.Windows 7软AP一个还是比较简单的.本文介绍在Ubuntu下实现软AP.(需要你的无线网卡支持AP哈) ...
- 理解Lucene中的Query
Query是一个接口,它有很多实现类. QueryParser是Query解析器,用于将一个字符串解析为一个Query对象,这个Query对象可能属于TermQuery,也可能属于PhraseQuer ...