给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。

示例 1:

输入: nums = [-1,0,3,5,9,12], target = 9 输出: 4 解释: 9 出现在 nums 中并且下标为 4

示例 2:

输入: nums = [-1,0,3,5,9,12], target = 2 输出: -1 解释: 2 不存在 nums 中因此返回 -1

提示:

  1. 你可以假设 nums 中的所有元素是不重复的。
  2. n 将在 [1, 10000]之间。
  3. nums 的每个元素都将在 [-9999, 9999]之间。
class Solution {
public:
int search(vector<int>& nums, int target) {
int high = nums.size() - 1;
int low = 0;
while(low <= high)
{
int mid = (low + high) / 2;
if(nums[mid] == target)
{
return mid;
}
else if(nums[mid] < target)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
};

Leetcode704.Binary Search二分查找的更多相关文章

  1. [01]Binary Search二分查找

    Binary Search二分查找 作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2) 1.Pyt ...

  2. LeetCode 704. Binary Search (二分查找)

    题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime:  0 ms, faster than 100 % Memory Usage ...

  3. lintcode:Binary Search 二分查找

    题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...

  4. STL模板整理 Binary search(二分查找)

    前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...

  5. 【算法模板】Binary Search 二分查找

    模板:(通用模板,推荐) 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. ...

  6. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  7. 501. Find Mode in Binary Search Tree查找BST中的众数

    [抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...

  8. Codeforces Round #678 (Div. 2) C. Binary Search (二分,组合数)

    题意:有长度\(n\)的序列,让你构造序列,使得二分查找能在\(pos\)位置找到值\(x\).问最多能构造出多少种排列? 题解:题目给出的\(pos\)是固定的,所以我们可以根据图中所给的代码来进行 ...

  9. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

随机推荐

  1. HandlerInterceptorAdapter或HandlerInterceptor的使用

    Spring拦截器 HandlerInterceptorAdapter需要继承,HandlerInterceptor需要实现 可以作为日志记录和登录校验来使用 建议使用HandlerIntercept ...

  2. linux系统下重要的分区及其作用

    下面列出来的是linux系统下重要的分区及其作用/bin :bin是binary的缩写;/boot :存放启动Linux时使用的一些核心文件;/root :root(超级管理员)的用户主目录;/sbi ...

  3. js节点

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. angularjs中动态为audio绑定src问题总结

    先上代码 <div class="block_area block_audio" ng-show="model.url"> <audio co ...

  5. iOS学习笔记-084.粒子效果——路径移动

    https://blog.csdn.net/qiwenmingshiwo/article/details/75806637 粒子效果路径移动一说明1 效果2 步骤分析二代码1 VCViewh2 VCV ...

  6. python 中文乱码问

    在本文中,以'哈'来解释作示例解释所有的问题,“哈”的各种编码如下: 1. UNICODE (UTF8-16),C854: 2. UTF-8,E59388: 3. GBK,B9FE. 一.python ...

  7. 09安装运行redis-trib.rb所需的环境

    运行redis-trib.rb脚本配置Redis的cluster,需要安装ruby环境,这里采用源码安装: 1:下载源码包: https://cache.ruby-lang.org/pub/ruby/ ...

  8. Spring.Net2.0+NHibernate4.0 +Asp.Net Mvc4 一

    1.创建项目结构 控制器:    SN.Controllers 数据访问 :SN.Dao 实体映射: SN.Models 服务层:     SN.Servers 视图层:   SN.Web 2.添加需 ...

  9. vue-cli3.x正确打包项目,解决静态资源与路由加载无效的问题,history模式下配合使用nginx运行打包后的项目

    使用vue-cli3.x正确打包项目,配合nginx运行打包后的内容 vue.config.js module.exports = { publicPath: './',//打包后的位置(如果不设置这 ...

  10. HDU3486 RMQ

    /*多么变态的一道题,交了18次*/ #include<cstdio> #include<cstring> #include<cmath> #define max( ...