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 ...
随机推荐
- Android Studio(Kotlin)之RecyclerView
RecyclerView应该是ListView的增强版. RecyclerView与ListView的区别(我认为的): RecyclerView的性能比ListView高 RecyclerView支 ...
- Linux中内存、CPU使用情况查看
1.背景 在实际生产中我们为了保证系统能稳定运行,我们经常要查看当前的CPU和系统使用情况 建议使用top,简单丰富,快捷 2.使用free查看内存使用情况 3.使用 top查看内存.cpu内存占比 ...
- coding如何绑定二次验证码_虚拟MFA_两步验证_身份验证?
Coding.net 是一个面向开发者的云端开发平台,提供 Git/SVN 代码托管.任务管理.在线 WebIDE.Cloud Studio.开发协作.文件管理.Wiki 管理.提供个人服务及企业管理 ...
- DJANGO-天天生鲜项目从0到1-014-订单-订单评论
本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...
- ~~网络编程(三):TCP/UDP~~
进击のpython ***** 网络编程--TCP/UDP协议 其实你也发现了,应用层是交给应用来处理的,我们什么也做不了 相较于网络编程来说,我们更重要的是在做应用层和传输层的对接 因为你也看到了, ...
- mysql字符集 utf8 和utf8mb4 的区别
一.导读我们新建mysql数据库的时候,需要指定数据库的字符集,一般我们都是选择utf8这个字符集,但是还会又一个utf8mb4这个字符集,好像和utf8有联系,今天就来解析一下这两者的区别. 二.起 ...
- 为PhpStorm添加Laravel 代码智能提示功能
php artisan clear-compiled //清除bootstrap/compiled.php php artisan ide-helper:generate //为 Facades 生成 ...
- LQB2013A02排它平方数
这个题方向其实还算好找,就是枚举嘛 (这是一个填空题,所以六个for嵌套也无所谓,因为毕竟emmmm,不看时间) 这里是判断的代码: 需要把数字转化成字符串 void i2s(int x,string ...
- SSM框架练习之Jsp页面使用taglib标签报错500的问题
最近在练手一个SSM的基于AdminLET框架模板的后台管理系统,使用的环境是tomcat9,使用Maven构建并通过添加Web模板框架的项目,在添加完所有的配置文件后启动tomcat运行,出现了一个 ...
- Python pass语句
Python pass语句:空语句,主要用于保持程序结构的完整性 或者 函数想要添加某种功能,但是还没有想好具体应该怎么写. 在 for 循环中使用 pass: lst = [7,8,9,4] for ...