C#LeetCode刷题之#704-二分查找(Binary Search)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3999 访问。
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。
输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4
输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不存在 nums 中因此返回 -1
提示:
- 你可以假设 nums 中的所有元素是不重复的。
- n 将在 [1, 10000]之间。
- nums 的每个元素都将在 [-9999, 9999]之间。
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Note:
- You may assume that all elements in nums are unique.
- n will be in the range [1, 10000].
- The value of each element in nums will be in the range [-9999, 9999].
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3999 访问。
public class Program {
public static void Main(string[] args) {
var nums = new int[] { -1, 0, 3, 5, 9, 12 };
var target = 9;
var res = Search(nums, target);
Console.WriteLine(res);
nums = new int[] { 1, 3, 5, 7, 9 };
target = 3;
res = Search2(nums, target);
Console.WriteLine(res);
Console.ReadKey();
}
private static int Search(int[] nums, int target) {
//暴力求解
for(var i = 0; i < nums.Length; i++) {
if(nums[i] == target) return i;
}
return -1;
}
private static int Search2(int[] nums, int target) {
//二分法
var low = 0;
var high = nums.Length - 1;
var mid = 0;
while(low <= high) {
mid = low + (high - low) / 2;
if(nums[mid] < target) {
low = mid + 1;
} else if(nums[mid] > target) {
high = mid - 1;
} else {
return mid;
}
}
return -1;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3999 访问。
4
1
分析:
显而易见,Search 的时间复杂度为: ,Search2 的时间复杂度为:
。
C#LeetCode刷题之#704-二分查找(Binary Search)的更多相关文章
- LeetCode 704. 二分查找(Binary Search)
704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...
- Leetcode之二分法专题-704. 二分查找(Binary Search)
Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 t ...
- STL之二分查找 (Binary search in STL)
STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...
- 【转】STL之二分查找 (Binary search in STL)
Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...
- 二分查找(binary search)
二分查找又叫折半查找,要查找的前提是检索结果位于已排序的列表中. 概念 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是 ...
- LeetCode算法题-Convert Sorted Array to Binary Search Tree(Java实现)
这是悦乐书的第166次更新,第168篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第25题(顺位题号是108).给定一个数组,其中元素按升序排序,将其转换为高度平衡的二叉 ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- C#LeetCode刷题之#35-搜索插入位置(Search Insert Position)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3979 访问. 给定一个排序数组和一个目标值,在数组中找到目标值, ...
- [Swift]LeetCode704. 二分查找 | Binary Search
Given a sorted (in ascending order) integer array nums of nelements and a target value, write a func ...
随机推荐
- Kafka 信息整理
请说明什么是传统的消息传递方法? 传统的消息传递方法包括两种: ·排队:在队列中,一组用户可以从服务器中读取消息,每条消息都发送给其中一个人. ·发布-订阅:在这个模型中,消息被广播给所有的用户. 为 ...
- Illegal instant due to time zone offset transition (Asia/Shanghai)_夏令时问题
项目报错信息: Connot parse "1991-04-14",illegal instant due to time zone offset transition(Asia/ ...
- Python 爬取 42 年高考数据,告诉你高考为什么这么难?
作者 | 徐麟 历年录取率 可能很多经历过高考的人都不知道高考的全称,高考实际上是普通高等学校招生全国统一考试的简称.从1977年国家恢复高考制度至今,高考经历了许多的改革,其中最为显著的变化就是录取 ...
- 事件循环 event loop 究竟是什么
事件循环 event loop 究竟是什么 一些概念 浏览器运行时是多进程,从任务管理器或者活动监视器上可以验证. 打开新标签页和增加一个插件都会增加一个进程,如下图:  浏览器渲染进程是多线程,包 ...
- Substance Designer学习资料参考及学习入门感受
先奉上大佬写的: 名称:Substance Designer 萌新入门流程 地址:https://zhuanlan.zhihu.com/p/56194917 作者:ShadowjackLeeSD小菜鸡 ...
- 食用Win系统自带的PowerShell登录服务器
运行powershell输入ssh root@你的服务器ip -p你的端口 切换rm ~/.ssh/known_hosts cmd 运行 ping 你的ip -t一直ping ctrl+c停止 tra ...
- 火狐浏览器如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器?
一般点账户名——设置——安全设置中开通虚拟MFA两步验证 具体步骤见链接 火狐浏览器如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器? 二次验证码小程序于谷歌身份验证器APP的优势 1.无需下载 ...
- java 成员变量和局部变量的区别
将对象的存储在数组中会报错 public static void main(String[] args) { ArrayList<Goods> arrayList = new ArrayL ...
- F - Maximal Intersection --------暴力求解题
You are given n segments on a number line; each endpoint of every segment has integer coordinates. S ...
- 关于SignalR 进行双向多步对话
关于ASP.NET SignalR 解释百度百科是这样说的: ASP.NET SignalR 是为 ASP.NET 开发人员提供的一个库,可以简化开发人员将实时 Web 功能添加到应用程序的过程.实时 ...