问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)相关推荐

  1. 卷进大厂系列之LeetCode刷题笔记:二分查找(简单)

    LeetCode刷题笔记:二分查找(简单) 学算法,刷力扣,加油卷,进大厂! 题目描述 涉及算法 题目解答 学算法,刷力扣,加油卷,进大厂! 题目描述 力扣题目链接 给定一个 n 个元素有序的(升序) ...

  2. 第1天-代码随想录刷题训练| 704二分查找、26移除元素

    文章目录 1. 二分查找704 扩展 2.移除元素 2.1数组理论基础 2.2 暴力解法 2.3双指针解法 2.4 扩展题 1. 二分查找704 原题链接 给定一个 n 个元素有序的(升序)整型数组 ...

  3. LeetCode刷题记录6——696. Count Binary Substrings(easy)

    LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...

  4. LeetCode刷题记录4——67. Add Binary(easy)

    LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...

  5. 二分查找(Binary Search)需要注意的问题,以及在数据库内核中的实现

    问题背景 今年的实习生招聘考试,我出了一道二分查找(Binary Search)的题目.题目大意如下: 给定一个升序排列的自然数数组,数组中包含重复数字,例如:[1,2,2,3,4,4,4,5,6,7 ...

  6. 八、二分查找(Binary Search)

    一.概述 二分查找(Binary Search,也称折半查找)--针对有序数据集合的查找算法 1.基本思想 类似分治思想,每次都通过跟区间的中间元素进行对比,将代查找的区间缩小为之前的一半,直到找到要 ...

  7. LeetCode LCP 12. 小张刷题计划(二分查找)

    1. 题目 为了提高自己的代码能力,小张制定了 LeetCode 刷题计划,他选中了 LeetCode 题库中的 n 道题,编号从 0 到 n-1,并计划在 m 天内按照题目编号顺序刷完所有的题目(注 ...

  8. LeetCode算法题4:二分查找及扩展应用

    文章目录 前言 一.二分查找 二.第一个错误的版本 三.搜索插入位置 总结 前言 Leetcode算法系列:https://leetcode-cn.com/study-plan/algorithms/ ...

  9. Leetcode中几道二分查找(Binary Search)的算法题总结

    二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列.二分查找法的时间复杂度是对数级别的,O(lo ...

  10. 算法导论第2章(3) 二分查找 binary search

    二分查找(分治法). 二分查找也是一种分治法的实现,每一次查找将数据分为两个部分,问题规模都减小一半.这样查找的时间复杂度为logN.因为其实查找过程建立了一棵有N个节点的二叉树,查找次数是这棵树的高 ...

最新文章

  1. oracle dba_tables各字段含义
  2. 编译内核出现make CONFIG_DEBUG_SECTION_MISMATCH=y错误
  3. 231 Power of Two 2的幂
  4. js能订阅mq吗_高颜值宠物营养品订阅盒!除了好看,真的能补充营养吗?
  5. MongoDB最佳实践(转)
  6. ABAP 标准培训教程 BC400 学习笔记之四:ABAP 编程语言的数据类型
  7. luogu4267 TamingtheHerd (dp)
  8. imx6 i2c分析
  9. ubuntu环境下如何安装jdk,安装eclipse,安装android studio总结
  10. Java中带标签的break,continue
  11. 错误./hello: error while loading shared libraries: libQtGui.so.4: cannot open shared object file:
  12. spring源码分析第六天------spring经典面试问题
  13. NoSQL和传统数据库的区别
  14. X.509证书代码解析
  15. Deepin安装搜狗中文输入法
  16. wintc的安装方法
  17. html插入图片后在网页显示不出来
  18. mariadb特有函数
  19. 王宇阳:六个案例里的SEO启发
  20. 计算机开关机操作记录,win7电脑操作记录怎么查看|win7查看电脑操作记录的方法...

热门文章

  1. SQL Server2008数据库的维护 (第四部分)
  2. STL——vector容器详解
  3. 【AI视野·今日CV 计算机视觉论文速览 第194期】Mon, 10 May 2021
  4. MxNet 迁移学习实现深度学习分类
  5. 【设计模式】设计模式C++编程实现之单例模式(Singleton Pattern)
  6. 对象流 ObjectInputStream java
  7. DataGridView控件
  8. 03-mysql的相关命令-启动与关闭服务-配置环境变量
  9. metronic 4.5.7开发环境下, 在Windows 10上安装了10.16.0版本的node js之后,导致node sass无法加载...
  10. 因政府禁令 微软 Windows 9 将作出重大调整