问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3979 访问。

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。

输入: [1,3,5,6], 5

输出: 2

输入: [1,3,5,6], 2

输出: 1

输入: [1,3,5,6], 7

输出: 4

输入: [1,3,5,6], 0

输出: 0


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.

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

Output: 2

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

Input: 1

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

Input: 4

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

Input: 0


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3979 访问。

  1. public class Program {
  2. public static void Main(string[] args) {
  3. int[] nums = { 1, 3, 5, 6 };
  4. Console.WriteLine(SearchInsert(nums, 2));
  5. Console.WriteLine(SearchInsert2(nums, 6));
  6. Console.ReadKey();
  7. }
  8. private static int SearchInsert(int[] nums, int target) {
  9. for(int i = 0; i < nums.Length; i++) {
  10. if(nums[i] >= target) return i;
  11. }
  12. return nums.Length;
  13. }
  14. private static int SearchInsert2(int[] nums, int target) {
  15. int mid = 0, low = 0;
  16. int high = nums.Length - 1;
  17. while(low <= high) {
  18. mid = low + (high - low) / 2;
  19. if(nums[mid] == target) {
  20. return mid;
  21. } else if(nums[mid] < target) {
  22. low = mid + 1;
  23. } else {
  24. high = mid - 1;
  25. }
  26. }
  27. return low;
  28. }
  29. }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3979 访问。

  1. 1
  2. 3

分析:

显而易见,SearchInsert在最坏的情况下的时间复杂度为:  , SearchInsert2在最坏的情况下的时间复杂度为:  。

C#LeetCode刷题之#35-搜索插入位置(Search Insert Position)的更多相关文章

  1. [Swift]LeetCode35. 搜索插入位置 | Search Insert Position

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

  2. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

  3. Leetcode题库——35.搜索插入位置

    @author: ZZQ @software: PyCharm @file: searchInsert.py @time: 2018/11/07 19:20 要求:给定一个排序数组和一个目标值,在数组 ...

  4. leetcode刷题-79单词搜索

    题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复 ...

  5. 【leetcode算法-简单】35. 搜索插入位置

    [题目描述] 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5, ...

  6. 【leetcode刷题笔记】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. 【leetcode刷题笔记】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  8. 【leetcode刷题笔记】Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  9. 【leetcode刷题笔记】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. 六十来行python代码完成一个文件分类器

    ​    你的桌面是否像这样的一样被各种文件给堆满了,但是每一个文件又不清楚是否后面还有作用,也不敢删除,自己一个一个转移又太麻烦了.没关系,今天我带大家用python一起来做一个文件归类器,一键进行 ...

  2. GPO - Backup and Restore

    Backup the GPO to a second server is very important. Restore a GPO if necessary. Note: WMI filter an ...

  3. 【软件安装】CentOS7_直播服务搭建_nginx_nginx-http-flv-module

    1.介绍 nginx-http-flv-module是在nginx-rtmp-module基础上开发的一个直播模块. 感谢Arut创造了nginx-rtmp-module,它是Nginx的一个优秀的第 ...

  4. 从0搭建一个基于 ELK 的日志、指标收集与监控系统

    为了使得私有化部署的系统能更健壮,同时不增加额外的部署运维工作量,本文提出了一种基于 ELK 的开箱即用的日志和指标收集方案. 在当前的项目中,我们已经使用了 Elasticsearch 作为业务的数 ...

  5. PHP常见的十个安全问题

    相对于其他几种语言来说, PHP 在 web 建站方面有更大的优势,即使是新手,也能很容易搭建一个网站出来.但这种优势也容易带来一些负面影响,因为很多的 PHP 教程没有涉及到安全方面的知识. 此帖子 ...

  6. GridMask:一种数据增强方法

    GridMask Data Augmentation, ARXIV 2020 代码地址:https://github.com/akuxcw/GridMask 这篇论文提出了一种简单的数据增强方法,在图 ...

  7. phpcms根据二级栏目列表写的三级栏目列表

    <div class="container"> <!--左边树状导航--> <div class="CNLTreeMenu" id ...

  8. PHP 是什么?简介下

    PHP 是服务器端脚本语言. 您应当具备的基础知识 在继续学习之前,您需要对以下知识有基本的了解: HTML CSS PHP 是什么? PHP(全称:PHP:Hypertext Preprocesso ...

  9. Python 字典(Dictionary) fromkeys()方法

    描述 Python 字典 fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值.高佣联盟 www.cgewang.com 语法 from ...

  10. UOJ 422 [集训队作业2018] 小Z的礼物 min-max容斥 期望 轮廓线dp

    LINK:小Z的礼物 太精髓了 我重学了一遍min-max容斥 重写了一遍按位或才写这道题的. 还是期望多少时间可以全部集齐. 相当于求出 \(E(max(S))\)表示最后一个出现的期望时间. 根据 ...