60. Search Insert Position 【easy】
60. Search Insert Position 【easy】
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.
[1,3,5,6]
, 5 → 2
[1,3,5,6]
, 2 → 1
[1,3,5,6]
, 7 → 4
[1,3,5,6]
, 0 → 0
O(log(n)) time
解法一:
class Solution {
/**
* param A : an integer sorted array
* param target : an integer to be inserted
* return : an integer
*/
public:
int searchInsert(vector<int> &A, int target) {
if (A.size() == ) {
return ;
} int start = ;
int end = A.size() - ; while (start + < end) {
int mid = start + (end - start) / ; if (A[mid] == target) {
return mid;
}
else if (A[mid] < target) {
start = mid;
}
else if (A[mid] > target) {
end = mid;
}
} if (target <= A[start]) {
return start;
}
else if (target <= A[end]) {
return end;
}
else if (target > A[end]) {
return end + ;
}
}
};
最后返回的时候要格外注意,按照模板来说,最后返回的start或者end中是有可能等于我们要找的数的,如果都没有找到后面还要补刀一个-1;对应这个题不用补刀,因为是要找插入的位置,必定会有一个合理的插入位置。
解法二:
public class Solution {
public int searchInsert(int[] A, int target) {
if (A == null || A.length == ) {
return ;
}
int start = ;
int end = A.length - ;
int mid; if (target < A[]) {
return ;
}
// find the last number less than target
while (start + < end) {
mid = start + (end - start) / ;
if (A[mid] == target) {
return mid;
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
} if (A[end] == target) {
return end;
}
if (A[end] < target) {
return end + ;
}
if (A[start] == target) {
return start;
}
return start + ;
}
}
解法三:
class Solution {
/**
* param A : an integer sorted array
* param target : an integer to be inserted
* return : an integer
*/
public:
int searchInsert(vector<int> &A, int target) {
// find first position >= target
if (A.size() == ) {
return ;
} int start = , end = A.size() - ;
while (start + < end) {
int mid = (end - start) / + start;
if (A[mid] >= target) {
end = mid;
} else {
start = mid;
}
} if (A[start] >= target) {
return start;
}
if (A[end] >= target) {
return end;
} return A.size();
}
};
大神解法,非常简便!
60. Search Insert Position 【easy】的更多相关文章
- 35. Search Insert Position【leetcode】
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 60.Search Insert Position.md
描述 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 您在真实的面试中是否遇到过这个题? 样例 Given ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- 14. First Position of Target 【easy】
14. First Position of Target [easy] For a given sorted array (ascending order) and a targetnumber, f ...
- 28. Search a 2D Matrix 【easy】
28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...
- LeetCode--Array--Remove Element && Search Insert Position(Easy)
27. Remove Element (Easy)# 2019.7.7 Given an array nums and a value val, remove all instances of tha ...
- 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 ...
随机推荐
- NOI2013部分题解
Day 1 T1:向量内积 直接暴力有60.发现将n个向量合成$n\times d$的矩阵$A$,然后求$A\times A^T$,得到的矩阵包含了所有的答案. 先考虑$k=2$,将答案矩阵和全1矩阵 ...
- python3开发进阶-Django框架学习前的小项目(一个简单的学员管理系统)
''' 自己独立写一个学员管理系统 表结构: 班级表: -id -grade_name 学生表: -id -student_name -grade 关联外键班级表 老师表: -id -teacher_ ...
- jdk8中Spliterator的作用
文章前半部分转自:https://blog.csdn.net/lh513828570/article/details/56673804 之前的时候看集合部分源码没看完,今天又翻了一下,看到了个东西sp ...
- 打印不同的数 Exercise07_05
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:打印不同的数 * */ public class Exercise07_0 ...
- java单例支持高并发
单例对象(Singleton)是一种常用的设计模式.在Java应用中,单例对象能保证在一个JVM中,该对象只有一个实例存在.这样的模式有几个好处: 1.某些类创建比较频繁,对于一些大型的对象,这是一笔 ...
- JavaScript对JSON数据进行排序
var ajson= { "result":[ { "cid":1, "name":"aaa", "price ...
- .NET反编译
http://www.cnblogs.com/powertoolsteam/archive/2011/01/05/1926066.html
- 转载:oracle11G 已开启监听,但远程连接依旧无监听解决过程
1.连接数据库显示无监听程序,首先查看服务器的oracle监听服务是否开启,服务名称:OracleOraDb11g_home1TNSListener(具体环境中可能不完全一样,但是认准TNSListe ...
- python-sdk-demo的打包
1.安装setuptools pip install python-setuptools 2.创建一个简单的包 下载demo https://github.com/cp-m/py-sdk-demo.g ...
- jenkins报错 not a queue url
使用Python的jenkinsapi执行job时报错:not a queue url 虽然任务还是构建了,但是错误还是处理的. 原因是:Jenkins的配置,和jenkinsapi里的配置的URL内 ...