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.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Note:

  1. You may assume that all elements in nums are unique.

  2. n will be in the range [1, 10000].
  3. The value of each element in nums will be in the range [-9999, 9999].

想法:一般的二分法查找步骤

class Solution {
public:
    int search(vector<int>& nums, int target) {
        if(nums.empty())
            ;
        ;
        ;
        while(left <= right){
            ;
            if(nums.at(mid) == target)
                return mid;
            if(nums.at(mid) > target){
                right = mid-;
            }else{
                left = mid+;
            }
        }
        ;
    }
};

leetcode704--Binary Search的更多相关文章

  1. Leetcode704.Binary Search二分查找

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

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

    704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...

  3. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  4. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  5. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  6. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  7. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  8. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  9. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  10. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. Java - HashSet源码解析

    java提高篇(二四)-----HashSet 一.定义 public class HashSet<E> extends AbstractSet<E> implements S ...

  2. js-ES6学习笔记-Promise对象(2)

    1.Promise实例具有then方法,也就是说,then方法是定义在原型对象Promise.prototype上的.它的作用是为Promise实例添加状态改变时的回调函数. 2.Promise.pr ...

  3. 移动端开发时默认样式reset

    /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, ...

  4. Maven学习(一)概念简述和安装教程

    刚开始由.net转向java,公司的项目中就开始使用maven,感觉他就像nuget一样,但是他又比nuget要强大(毕竟他是项目管理工具). maven概述 在这里,就不背书包了,怎么简单直接怎么来 ...

  5. 活字格Web应用平台学习笔记5 - 编辑和删除记录

    了几天,今天上活字格网站一看,他们获奖了,好厉害 荣膺盘古奖!活字格引领企业信息化建设新潮流 好吧,我继续学习,希望早点拿到认证证书. 今天要学的是编辑删除记录.目标: 还是接续之前的工程.做好后是这 ...

  6. PHPCMS V9开发文档

    内容模块 内容模块PC标签调用说明 模块名:content 模块提供的可用操作 操作名 说明 lists 内容数据列表 relation 内容相关文章 hits 内容数据点击排行榜 category ...

  7. Asp.net MVC检测到有潜在危险的 Request.Form 值

    解决方法很简单,不像网上说的那么麻烦.只需一步: [ValidateInput(false)] public ActionResult Test(){ }

  8. vs2017安装cuda9.0编译默认示例失败解决方法

    https://devtalk.nvidia.com/default/topic/1027209/cuda-setup-and-installation/cuda-9-0-does-not-work- ...

  9. C#中使用反射遍历一个对象属性和值以及百分数

    对某个类的实例化对象, 遍历获取所有属性(子成员)的方法(采用反射): using (var context = new YZS_TRAEntities()) { ).FirstOrDefault() ...

  10. 通用视图-分开处理GET、POST请求

    1.编辑urls.py: url(r'^vmmgmt-vms/$', views.vmmgmt,{'model':models.hvvms,'GET': views.vmmgmt_get_view,' ...