[LeetCode&Python] Problem 704. 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 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(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        n=len(nums)
        low=0
        high=n-1
        mid=(low+high)//2

        while low!=mid:
            if nums[mid]==target:
                return mid
            elif nums[mid]>target:
                high=mid
                mid=(high+low)//2
            elif nums[mid]<target:
                low=mid
                mid=(high+low)//2
        if nums[mid]==target:
            return mid
        elif nums[high]==target:
            return high
        else:
            return -1

  

原文地址:https://www.cnblogs.com/chiyeung/p/10180672.html

时间: 2024-11-05 21:58:06

[LeetCode&Python] Problem 704. Binary Search的相关文章

LeetCode 704. Binary Search

704. Binary Search(二分查找) 链接 https://leetcode-cn.com/problems/n-repeated-element-in-size-2n-array 题目 给定一个?n?个元素有序的(升序)整型数组?nums 和一个目标值?target ?,写一个函数搜索?nums?中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: nums = [-1,0,3,5,9,12], target = 9 输出: 4 解释: 9 出现在 nu

LeetCode详细分析 :: Recover Binary Search Tree [Tree]

Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

【一天一道LeetCode】#96. Unique Binary Search Trees

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example, Given n = 3, there are a total of 5 unique BST's. (二)解题

leetcode第一刷_Unique Binary Search Trees

这道题其实跟二叉搜索树没有什么关系,给定n个节点,让你求有多少棵二叉树也是完全一样的做法.思想是什么呢,给定一个节点数x,求f(x),f(x)跟什么有关系呢,当然是跟他的左右子树都有关系,所以可以利用其左右子树的结论,大问题被成功转化成了小问题.最熟悉的方法是递归和dp,这里显然有大量的重复计算,用dp打表好一些. 后来实验的同学说,这其实是一个Catalan数,上网查了一下,果然啊.Catalan数是这样子的: h(0) = 1, h(1) = 1; 递推式:h(n)= h(0)*h(n-1)

leetcode第一刷_Unique Binary Search Trees II

http://acm.hdu.edu.cn/showproblem.php?pid=1507 大致题意:在一个n*m的格子上,黑色的地方不可用,问在白色格子上最多可放多少1*2的矩阵. 思路:建图,每个白色格子与它临近的上下左右的白色格子建边,求最大匹配,答案为最大匹配/2,因为是双向图.最后输出匹配边时,当找到一组匹配边记得将该边标记,以防重复计算. #include <stdio.h> #include <algorithm> #include <set> #inc

leetcode第一刷_Recover Binary Search Tree

这是一道好题,思路虽然有,但是提交之后总是有数据过不了,又按照数据改改改,最后代码都没法看了.收到的教训是如果必须为自己的代码加上很多很多特殊的限定,来过一些特殊的数据的话,说明代码本身有很大的漏洞. 这道题,我想到了要用两个指针保存乱序的节点,甚至想到了用一个pre指针来保存前面一个节点,但是问题出在哪里呢?我觉得应该是自己对树的遍历理解的不够深刻.既然知道了二叉搜索树一定是用中序遍历的,那么程序的框架应该马上写的出来,先左子树,再根,再右子树,那你说什么时候更新pre指针呢,当然是访问根节点

[LeetCode&amp;Python] Problem 700. Search in a Binary Search Tree

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL. For exampl

[LeetCode&amp;Python] Problem 235. 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 BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has

【LeetCode】Balanced Tree &amp; Binary Search Tree

整合两道差不多的题目放上来,其中第一题是第二题的基础. 1. Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than one. 所谓