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. 【点分治】hdu5016 Mart Master II

    点分治好题. ①手动开栈. ②dp预处理每个点被哪个市场控制,及其距离是多少,记作pair<int,int>数组p. ③设dis[u].first为u到重心s的距离,dis[u].seco ...

  2. CentOS正确关机方法(转)

    CentOS正确关机方法 1关机前准备 1.1观察系统使用状态 ·         谁在线:who ·         联网状态:netstat -a ·         后台执行的程序:ps -au ...

  3. 显示/隐藏Mac系统中所有的隐藏文件

    显示: 在终端输入:defaults write com.apple.finder AppleShowAllFiles YES 隐藏: 在终端输入:defaults write com.apple.f ...

  4. Jquery radio选中

    radio选中$("input[name=test][value=34]").attr("checked",true);//value=34的radio被选中$ ...

  5. TSQLDBServerHttpApi一个使用HTTPS.SYS内核的数据库服务端控件

    TSQLDBServerHttpApi一个使用HTTPS.SYS内核的数据库服务端控件 在WINDOWS操作系统,TSQLDBServerHttpApi使用HTTPS.SYS作为通信内核,而HTTPS ...

  6. CentOS6 安装golang

    CentOS6 安装golang 下载 wget http://golangtc.com/static/go/1.8/go1.8.linux-amd64.tar.gz 1 1 1 解压 tar -xz ...

  7. 通过API获取 Portus+registry docker仓库信息

    接口获取docker centos 镜像的 tag 列表脚本 # -*- encoding:utf-8 -*- import requests import json ""&quo ...

  8. scrapy-splash抓取动态数据例子十二

    一.介绍 本例子用scrapy-splash通过搜狗搜索引擎,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二. ...

  9. 怎样编写高效android代码

    基于Android相关设备作为嵌入式设备范畴,在书写App应用的时候要格外关注效率.而且受电池电量的限制.这就导致嵌入式设备有诸多考虑.有限处理能力.因此就要求我们尽量去写高效的代码. 本文讨论了非常 ...

  10. Kubernetes概念介绍和v1版本部署过程

    简介: k8s一个开源的,跨主机管理容器应用集群的编排系统,为应用提供了基础的部署.维护和扩缩容机制. 编排:跨Docker主机同一管理容器集群. 目的 简化开发和运维容器集群的工作. 让开发和运维能 ...