leetcode-algorithms-35 Search Insert Position

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:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0

解法

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if (nums.size() <= 0 || nums[0] >= target) return 0;
for (int i = 0; i < nums.size(); ++i)
{
if (nums[i] == target) return i;
if (i + 1 < nums.size() && nums[i] < target && nums[i + 1] > target) return i + 1;
}
return nums.size();
}
};

时间复杂度: O(n).

空间复杂度: O(1).

链接: leetcode-algorithms 目录

leetcode-algorithms-35 Search Insert Position的更多相关文章

  1. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

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

  3. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

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

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

  5. Leetcode No.35 Search Insert Position(c++实现)

    1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...

  6. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  7. LeetCode OJ 35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. LeetCode Problem 35:Search Insert Position

    描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...

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

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

  10. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

随机推荐

  1. HTTPS 如何保证数据传输的安全性

    为什么需要 HTTPS? 我们知道 HTTP 是一个纯文本传输协议,对传输过程中的数据包不进行加密,是明文传输,那这样的话对于介于在发送端和接收端之间的任何 一个节点都能知道传输的内容,这些节点可能是 ...

  2. python3 session cookie

    session是保存在服务器中的,而cookies是保存在客户端中的.服务器通过session id来跟踪客户,而对于客户端而言,session id是保存在cookies中的,所以只要把cookie ...

  3. 一.移动app测试与质量保证

    1.典型的互联网产品的研发流程,及其核心做法.这里并不是简单的套用敏捷等流程方法,而是经过时间摸索和不断调整,找到最适合自己产品的流程做法,这是质量实践质量保证的基础. 2.系统功能测试实践.包涵需求 ...

  4. virtualbox中的虚拟机和windows共享文件夹

    http://www.jianshu.com/p/4e3c8b06cb06 为什么要共享文件夹? 在工作的过程当中会使用到不同的软件开发环境,php的,python的,nodejs的为了隔离这些应用环 ...

  5. python 传递多个参数

    def oper(a,*args): print(args) print(a) oper("q","s","d","z" ...

  6. DataGrip激活码

    引言: 网上有有很多datagirp的激活码,但是经过尝试很多都失效了,找了半天终于 找到了一个可用的激活码! 1. 激活码 适用版本: DataGrip2018.2.3,2018.1.1,其他版本没 ...

  7. Utunbu常见问题

    关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案 https://blog.csdn.net/u011596455/article/details/ ...

  8. 一篇好文之Android数据库 GreenDao的完全解析

    数据库GreenDao.jpg 之前在开发过程中,数据库基本上会使用Litepal或者SQlite自己写,最近换新环境,公司原先使用的数据库就是GreenDao,在各种情况的作用下,准备了解下Gree ...

  9. 基于虹软 2.0 人脸识别猜年龄 java版 demo

    首先感谢虹软,是你们提供这么好的SDK支撑了我们的想象力! 这是一个用javav编写的可视化应用,用户通过自己的脸和计算机进行交互,计算机则通过萌萌女孩的语音和用户对话.核心程序就是利用ArcFace ...

  10. Corrupted Metadata/failed to mount /sysroot

    公司电脑CentOS系统突然断电,开机后,无法进入系统.查看系统log如下: mount: mount /dev/mapper/cl-root on /sysroot failed:Structure ...