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. 怎么在父窗口调用它页面的iframe里面数据,进行操作?

    注:在服务器下操作有效果,本地无效 document.getElementById("taskdetail1").contentWindow.test(a) document.ge ...

  2. Docker 开发最佳实践

    Docker development best practices The following development patterns have proven to be helpful for p ...

  3. 【Python】【容器 | 迭代对象 | 迭代器 | 生成器 | 生成器表达式 | 协程 | 期物 | 任务】

    Python 的 asyncio 类似于 C++ 的 Boost.Asio. 所谓「异步 IO」,就是你发起一个 IO 操作,却不用等它结束,你可以继续做其他事情,当它结束时,你会得到通知. Asyn ...

  4. .Net MVC关于子页面引用js库问题

    layout页面中的配置: @RenderSection("scripts", required: false) @RenderSection("Styles" ...

  5. docker 日志分析

    日志分两类,一类是 Docker 引擎日志:另一类是 容器日志. Docker 引擎日志 Docker 引擎日志 一般是交给了 Upstart(Ubuntu 14.04) 或者 systemd (Ce ...

  6. python requests post和get

    import requests import time import hashlib import os import json from contextlib import closing impo ...

  7. python 部分函数

    abs(number) ,返回数字的绝对值cmath.sqrt(number) ,返回平方根,也可以应用于负数float(object) ,把字符串和数字转换为浮点数help() ,提供交互式帮助in ...

  8. python os.remove

    remove 只能删除文件,删除目录会报错 >>> import os >>> os.remove("/opt/xxx/server_log/test&q ...

  9. JAVA SE ArrayList 底层实现

    Array 查询效率高,增删效率低( Link 增删效率高 Vector 线程安全 List 列表 源代码: package com.littlepage.test; /** * 基于底层实现Arra ...

  10. html5 history 信息api pushState

    这个功能可以进行传参,还可以解决ajax无法前进和倒退的问题 首先,history新增的两个方法history.replaceState()和history.pushState()方法属于HTML5浏 ...