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.

Example

[1,3,5,6], 5 → 2

[1,3,5,6], 2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6], 0 → 0

Challenge

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】的更多相关文章

  1. 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 ...

  2. 60.Search Insert Position.md

    描述 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 您在真实的面试中是否遇到过这个题? 样例 Given ...

  3. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  4. 【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 ...

  5. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  6. 14. First Position of Target 【easy】

    14. First Position of Target [easy] For a given sorted array (ascending order) and a targetnumber, f ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. [BZOJ 3152] 组合子逻辑

    Link: BZOJ 3152 传送门 Solution: 喜闻乐见,gyz出的语文题,题意要看半小时 题意:使用最少的括号将序列分割,对于每个括号中的序列,设最左边的数为$num$,序列中元素个数为 ...

  2. newcoder contest 114 B - 求值2

    记得这是一个组合数卷积的板子题. ∑ C(A,i) * C(B,D-i)   =  C(A+B,D) 然后就直接做了. #include<cstdio> #include<cctyp ...

  3. 【字符串哈希】【BKDRhash】【Rabin-Karp算法】模板

    #include<cstdio> #include<iostream> #include<cstring> #include<string> #incl ...

  4. 【强联通分量缩点】【最短路】【spfa】bzoj1179 [Apio2009]Atm

    缩点后转化成 DAG图上的单源最长路问题.spfa/dp随便. #include<cstdio> #include<queue> #include<algorithm&g ...

  5. 【R笔记】R的内存管理和垃圾清理

    笔记: 1.R输入命令时速度不要太快,终究是个统计软件,不是编程! 2.memory.limit()查看当前操作系统分配内存给R的最大限度(单位是M?) 3.要经常 rm(object) 或者 rm( ...

  6. glib wpa_supplicant Unix上库编译错误解决与总结

    编译Linux下的库是一件痛苦的事情,这里主要阐述glib和wpa_supplicant库的编译,因各自的依赖关系,另外一些库要事先编译.glib依赖libffi和zlib,而wpa_supplica ...

  7. delphi设计浮动窗口

    delphi设计浮动窗口 用过Photoshop的朋友一定对它的那些方便的浮动面板记忆犹新,其实这些面板就是一个个的小窗体,但这些小窗体都放在Photoshop的主窗体上 (不是存在主窗体中),有自己 ...

  8. Visual Studio 控制台应用程序 同时使用OpenCV和matlab mat文件操作

    matalb具有灵活的图像处理,代码编写起来简洁而高效.而OpenCV具有很多成熟的计算机视觉算法,能够处理很多实时的识别处理等问题,而且代码运行起来效率很高.所以如何结合两者之间的优点,是让很多学术 ...

  9. leetcode练习之No.7------ 翻转整数reverse_integer

    原文地址:http://www.niu12.com/article/48 git地址:git@github.com:ZQCard/leetcode.git 给定一个 32 位有符号整数,将整数中的数字 ...

  10. 【Todo】Nodejs学习计划

    /Users/baidu/Documents/Data/Interview/Web-Server开发/深入浅出Node.js-f46c.pdf /Users/baidu/Documents/Data/ ...