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 ...
随机推荐
- luogu P1009 阶乘之和
题目描述 用高精度计算出S=1!+2!+3!+…+n!(n≤50) 其中“!”表示阶乘,例如:5!=5*4*3*2*1. 输入输出格式 输入格式: 一个正整数N. 输出格式: 一个正整数S,表示计算结 ...
- [Codeforces 15E] Triangle
Brief Introduction: 求从N出发,回到N且不包含任何黑色三角的路径数 Algorithm:假设从N点到第二层中间的节点M的路径数为k,易知总路径数为(k*k+1)*2 而从第第四层开 ...
- 【KMP】BZOJ3670-[Noi2014]动物园
[题目大意][依然借用别人的概括]给定一个长为L的字符串(L<=100W),求一个num数组,num[i]表示长度为i的前缀中字符串S’的数量,其中S‘既是该前缀的前缀也是该前缀的后缀,且|S' ...
- [BZOJ5358]/[HDU6287]口算训练
[BZOJ5358]/[HDU6287]口算训练 题目大意: 给定一个长度为\(n(n\le10^5)\)的正整数序列\(a_{1\sim n}\),\(m(m\le10^5)\)次询问.每次询问给出 ...
- 1.1(Spring MVC学习笔记)初识SpringMVC及SpringMVC流程
一.Spring MVC Spring MVC是Spring提供的一个实现了web MVC设计模式的轻量级Web框架. Spring优点:网上有,此处不复述. 二.第一个Spring MVC 2.1首 ...
- 每天一个linux命令12之top
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...
- 命令行下的C++程序转换成VC的MFC程序需要注意的问题
在将命令行下的C++程序转换成MFC窗口程序时一般会提示下面这种错误: fatal error C1010: unexpected end of file while looking for prec ...
- mysql-connector-java-6日期存储时差的问题解决方法
在my.ini文件中的[mysqld]下面加入 default-time_zone = '+8:00' 重启mysql 修改driver连接路径 这样日期保存到mysql就是正确的了,但是mysql- ...
- WebForm页面使用Ajax
AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML.指一种创建交互式网页应用的网页开发技术.AJAX并非缩写词,而是由Jesse ...
- coco2dx jni 调用 java 相机返回 图片数据
新建 一个项目 名字:testJin 包名:com.TanSon.org python命令:python create_project.py -project testJin -package c ...