leetcode704--Binary Search
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 innumsand its index is 4
Example 2:
Input:nums= [-1,0,3,5,9,12],target= 2 Output: -1 Explanation: 2 does not exist innumsso return -1
Note:
- You may assume that all elements in
numsare unique. nwill be in the range[1, 10000].- The value of each element in
numswill 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的更多相关文章
- Leetcode704.Binary Search二分查找
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...
- LeetCode 704. 二分查找(Binary Search)
704. 二分查找 704. Binary Search 题目描述 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target,写一个函数搜索 nums 中的 target,如果 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 判断ArryaList有没有重复对象的方法
ArrayList类是List类下一种常用的子类,如果要判断容器里面的对象是否有相等,有两种方法. 下面是自定义的一个Student类,假设容器里重复是按照对象的两个属性都相等. /** * @aut ...
- mongodb命令(持续更新)
$lt $lte $gt $gte< . <= . > . >= 1.分组求和db.collection.aggregate([{$group : {_id : null, s ...
- POJ3261(SummerTrainingDay10-G 后缀数组)
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 15974 Accepted: 7041 Ca ...
- mysql小试题
1. 用户登录日志表 xes_user_login_logs 如下: (1) 检索登录超过两次的用户ID(sql语句) select user_id from vvt_ceshi group by u ...
- python并发编程-线程和锁
什么是线程 进程:资源分配单位 线程:cpu执行单位(实体),每一个py文件中就是一个进程,一个进程中至少有一个线程 线程的两种创建方式: from multiprocessing import Pr ...
- Python selenium —— 父子、兄弟、相邻节点定位方式详解
今天跟大家分享下selenium中根据父子.兄弟.相邻节点定位的方法,很多人在实际应用中会遇到想定位的节点无法直接定位,需要通过附近节点来相对定位的问题,但从父节点定位子节点容易,从子节点定位父节点. ...
- SPOJ4580 ABCDEF(meet in the middle)
题意 题目链接 Sol 发现abcdef是互不相关的 那么meet in the middle一下.先算出abc的,再算def的 注意d = 0的时候不合法(害我wa了两发..) #include&l ...
- js的style.display小问题
在元素添加class样式隐藏display:none; 使用console.log(xx.style.display);//空值
- Asp Url汉字乱码的问题
1.js <a target="_blank" href="/asp/download.asp?File=' + escape(item.FileName) + ' ...
- 葡萄城报表介绍:矩表(Tablix)
一.矩表(Tablix)定义 Tablix 即表(Table)+矩阵(Matrix)+列表(List),它是报表中展示数据的一种工具,由表.矩阵.列表这三个具有相似性的报表项组成. 二.矩表(Tabl ...