C#LeetCode刷题之#35-搜索插入位置(Search Insert Position)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 访问。
public class Program {
public static void Main(string[] args) {
int[] nums = { 1, 3, 5, 6 };
Console.WriteLine(SearchInsert(nums, 2));
Console.WriteLine(SearchInsert2(nums, 6));
Console.ReadKey();
}
private static int SearchInsert(int[] nums, int target) {
for(int i = 0; i < nums.Length; i++) {
if(nums[i] >= target) return i;
}
return nums.Length;
}
private static int SearchInsert2(int[] nums, int target) {
int mid = 0, low = 0;
int high = nums.Length - 1;
while(low <= high) {
mid = low + (high - low) / 2;
if(nums[mid] == target) {
return mid;
} else if(nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3979 访问。
1
3
分析:
显而易见,SearchInsert在最坏的情况下的时间复杂度为: , SearchInsert2在最坏的情况下的时间复杂度为:
。
C#LeetCode刷题之#35-搜索插入位置(Search Insert Position)的更多相关文章
- [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 ...
- Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)
Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...
- Leetcode题库——35.搜索插入位置
@author: ZZQ @software: PyCharm @file: searchInsert.py @time: 2018/11/07 19:20 要求:给定一个排序数组和一个目标值,在数组 ...
- leetcode刷题-79单词搜索
题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复 ...
- 【leetcode算法-简单】35. 搜索插入位置
[题目描述] 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5, ...
- 【leetcode刷题笔记】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【leetcode刷题笔记】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【leetcode刷题笔记】Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【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 ...
随机推荐
- It is indirectly referenced from required .class files错误查找的解决办法如下
It is indirectly referenced from required .class files 原因:是JDK引入有问题导致的 解决方案:我之前是错误的引入成了JRE 坑哇!!!,改成如 ...
- Atlassian Confluence 5.1.2 破解版部署
Atlassian Confluence(简称Confluence)是一个专业的wiki程序.它是一个知识管理的工具,通过它可以实现团队成员之间的协作和知识共享.Confluence 不是一个开源软件 ...
- 2万字长文包教包会 JVM 内存结构 保姆级学习笔记
写这篇的主要原因呢,就是为了能在简历上写个"熟悉JVM底层结构",另一个原因就是能让读我文章的大家也写上这句话,真是个助人为乐的帅小伙....嗯,不单单只是面向面试学习哈,更重要的 ...
- Makefile中的奇葩字符
% : Makefile规则通配符,一般出现在目标或是依赖中 * : shell命令中的通配符,一般出现在命令中 $@:目标的名字 $^:所有依赖的名字 $<:第一个依赖的名字 $?:所有依赖中 ...
- 火狐浏览器如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器?
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 火狐浏览器如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器? 二次验证码小程序于谷歌身份验证器APP的优势 1.无需下载 ...
- linux nginx 部署多套服务(以react包为例)
前言 今天我特地写下笔记,希望可以完全掌握这个东西,也希望可以帮助到任何想对学习这个东西的同学. 本文用nginx部署服务为主要内容,基于CentOs 7.8系统. 文档版本:1.0.1 更新时间:2 ...
- 前端学习(十):CSS选择器
进击のpython ***** 前端学习--CSS选择器 每一条CSS样式声明由两部分组成: 选择器{ 样式: } 在CSS中{}之前的部分就是"选择器","选择器&qu ...
- C语言学习笔记二---数据类型运算符与表达式
一.C的基本语法单位 1.标识符:有效长度:31(DOS环境下) 2.关键字:main不是 3.分隔符:空格符,制表符,换行符,换页符 4.注释符:a./*.....*/ b.// 二.C的常用输 ...
- 完成的设备扫描项目的几个关键程序,包括activity之间的转换
module 的 gradle.build最后三行的compile 是关键dependencies { implementation fileTree(dir: 'libs', include: [' ...
- NGINX 上的限流
NGINX 上的限流(译) zlup YP小站 今天 前言 本文是对Rate Limiting with NGINX and NGINX Plus的主要内容(去掉了关于NGINX Plus相关内容) ...