LeetCode 704. Binary Search (二分查找)
题目标签:Binary Search
很标准的一个二分查找,具体看code。
Java Solution:
Runtime: 0 ms, faster than 100 %
Memory Usage: 39 MB, less than 90 %
完成日期:07/31/2019
关键点:二分查找
class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(nums[mid] == target)
return mid;
else if(nums[mid] < target) // if mid is less than target, move to right half
left = mid + 1;
else // if mid is greater than target, move to left half
right = mid - 1;
}
return -1;
}
}
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 704. Binary Search (二分查找)的更多相关文章
- [01]Binary Search二分查找
Binary Search二分查找 作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2) 1.Pyt ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- [LeetCode] 704. Binary Search
Description Given a sorted (in ascending order) integer array nums of n elements and a target value, ...
- lintcode:Binary Search 二分查找
题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...
- STL模板整理 Binary search(二分查找)
前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...
- 【算法模板】Binary Search 二分查找
模板:(通用模板,推荐) 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. ...
- Leetcode704.Binary Search二分查找
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
随机推荐
- 每天一个Linux命令:ls(1)
ls ls命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录). 格式 ls [-alrtAFR] [name...] 参数选项 参数 备注 -a 列出目录下的所有文件,包括以 . ...
- 关于Web前端密码加密是否有意义的总结
关于Web前端密码加密是否有意义的总结 : https://blog.csdn.net/hla199106/article/details/45114801 个人:加密涉及到的是前后端的数 ...
- table标签详解
1.table标签中没有 tbody标签,浏览器会自动加上去的 2.一般表格的布局可以不使用 thead.tfoot 以及 tbody 元素.这样浏览器解析时会自动给一个 tbody标签的. 完整的 ...
- 如何禁止C++默认成员函数
如何禁止C++默认成员函数 发表于 2016-03-02 | 分类于 C++ | 阅读次数 17 前言 前几天在一次笔试过程中被问到C++如何设计禁止调用默认构造函数,当时简单的想法是直 ...
- vc/atlmfc/include/afx.h(24) : fatal error C1189: #error : Building MFC application with /MD[d] (CRT
环境:win7,64位,vs2012 1> c:/program files/microsoft visual studio 8/vc/atlmfc/include/afx.h(24) : fa ...
- 18、Page Object 设计模式
Page Object 设计模式的优点如下: 减少代码的重复. 提高测试用例的可读性. 提高测试用例的可维护性, 特别是针对 UI 频繁变化的项目. 当你针对网页编写测试时,你需要引用该网页中的元素, ...
- Spring-Boot使用neo4j-java-driver-- 查找两个节点之间关系的最短路径
一.Cypher数据 create (小北:朋友圈{姓名:"小北", 喜欢的书类:"Poetry"}), (小菲:朋友圈{姓名:"小菲", ...
- mongo之$max
原集合: { _id: 1, highScore: 800, lowScore: 200 } 应用: #意思是:更新_id 等于1 的记录,条件是highScore 950>原纪录的highSc ...
- android应用的资源
应用资源可以分为两大类: 1.无法直接访问的原生资源,保存在asset目录下. 2.可以通过R资源清单类访问的资源,保存在res目录下. 资源的类型以及存储方式: android要求在res目录下用不 ...
- 2018-8-10-win10-uwp-反射
title author date CreateTime categories win10 uwp 反射 lindexi 2018-08-10 19:17:19 +0800 2018-2-13 17: ...