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. luogu P1126 机器人搬重物

    题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有些格子为不可移动的障碍.机 ...

  2. 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons

    从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...

  3. windows下虚拟环境安装方法

    方法一:使用virtualenv安装虚拟环境 1.安装virtualenv pip install virtualenv 2.随意创建文件夹(将来虚拟环境会在这个文件夹)    mkdir mypro ...

  4. 1.4(Spring学习笔记)Spring-JDBC基础

    一.Spring JDBC相关类 1.1 DriverManagerDataSource DriverManagerDataSource主要包含数据库连接地址,用户名,密码. 属性及含义如下配置所示: ...

  5. KVC与KVO的不同

    vc 就是一种通过字符串去间接操作对象属性的机制,  访问一个对象属性我们可以 person.age  也可以通过kvc的方式   [person valueForKey:@"age&quo ...

  6. linux-改变文件属主权限-chown

    http://www.cnblogs.com/peida/archive/2012/12/04/2800684.html chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID: ...

  7. Mongodb 学习笔记简介

    目录 1       准备工作... 5 1.1        相关网址... 6 1.1        下载安装... 6 1.1.1         下载:... 6 1.1.2         ...

  8. SQLite 使用技巧

    http://blog.csdn.net/beifengdelei/article/details/7166056 SQLite自增ID自段使用方法为 INTEGER PRIMARY KEY AUTO ...

  9. EF4.4 升级EF6.0问题总结

    如出现下面代码错误,基本可能确定EF数据库配置错误 在 System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderCompositeReso ...

  10. iOS:Masonry 英文原档介绍

    Masonry 英文原档介绍: Masonry is still actively maintained, we are committed to fixing bugs and merging go ...