704. 二分查找

704. Binary Search

题目描述

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

LeetCode704. Binary Search简单

示例 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] 之间。

Java 实现

class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int i = 0, j = n, m;
while (i < j) {
m = i + (j - i) / 2;
if (nums[m] == target) {
return m;
} else if (nums[m] < target) {
i = m + 1;
} else {
j = m;
}
}
return -1;
}
}

相似题目

参考资料

LeetCode 704. 二分查找(Binary Search)的更多相关文章

  1. STL之二分查找 (Binary search in STL)

    STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...

  2. Java实现 LeetCode 704 二分查找(二分法)

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

  3. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  4. 二分查找(binary search)

    二分查找又叫折半查找,要查找的前提是检索结果位于已排序的列表中. 概念 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是 ...

  5. [Swift]LeetCode704. 二分查找 | Binary Search

    Given a sorted (in ascending order) integer array nums of nelements and a target value, write a func ...

  6. 数据结构-二分查找(Binary Search)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

  7. LeetCode 704.二分查找(C++)

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

  8. LeetCode 704. 二分查找

    题目链接:https://leetcode-cn.com/problems/binary-search/ 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函 ...

  9. Leetcode之二分法专题-704. 二分查找(Binary Search)

    Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 t ...

随机推荐

  1. django-全文解锁和搜索引擎

    安装和配置 全文检索安装 pip install django-haystack==2.5.1 # 2.7.0只支持django1.11以上版本 搜索引擎安装 pip install whoosh 安 ...

  2. MySQL 日期格式化,取年月日等相关操作

    日期取年.月.日 select id, phone,time,year(time),month(time), DAY(time),TIME(time) from user where phone='x ...

  3. 事件类型(onchange)

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

  4. Postgresql operator does not exist: numeric = character varying

    )),series_name from svcm_t_series_res: 原来series_id是numeric型,现在转换为varchar(64):

  5. 查vue版本号

    在项目中,找到package.json文件夹 找"dependencies"然后就可以看到你装的vue的版本了.

  6. art-template模板引擎高级使用

    一.结合express的基本使用 // npm下载express/art-template/express-art-tempalte,并且加载 var express=require('express ...

  7. shell 修改文件所有者

    chown   用户名   文件名  -R

  8. c++ 获取字符串中最长的回文子串

    #include <vector> #include <iostream> #include <string> using namespace std; strin ...

  9. ICEM-哑铃

    原视频下载地址:https://pan.baidu.com/s/1kVBKJbT ;密码: jqeh

  10. Java中在时间戳计算的过程中遇到的数据溢出问题

    背景 今天在跑定时任务的过程中,发现有一个任务在设置数据的查询时间范围异常,出现了开始时间戳比结束时间戳大的奇怪现象,计算时间戳的代码大致如下. package com.lingyejun.authe ...