Binary Search 专栏

Binary Search 时间复杂度 O(logN ), 因为每次减少一半 相当于取log

Q: 什么时候可以用Binary Seach?

A: 当数据是Sorted 并且支持Random Access的时候

Binary Search 的基本规则

1. 搜索空间在循环中不断减小

 The Searching Area decrease during the process

2. 目标元素(如果存在)不可以被排除到搜索空间之外

Basic Binary Search

 public int binarySearch(int[] array, int target) {
    //Corner case
    if(array==null || array.length==0){
      return -1;
    }
    int left=0;
    int right=array.length-1;
    while(left<=right){
      int mid=left+(right-left)/2;
      if(array[mid]==target){
        return mid;
      }else if(array[mid]>target){
        right=mid-1;
      }else{
        left=mid+1;
      }
    }
    return -1;
  }

注意几点常见错误

1. int mid=left+(right-left)/2;

目的是防止Overflow

2. 注意while 条件的判断 , 以下循环条件排列从苛刻到宽松

(1).while(left<=right)

留下0个元素

(2).while(left<right)

留下1个元素

(3).while(left<right-1)

留下两个元素

寻找最接近的元素index

 public int closest(int[] array, int target) {
    //Corner case
    if(array==null || array.length==0){
      return -1;
    }
    int left=0;
    int right=array.length-1;
    while(left<right-1){
      int mid=left+(right-left)/2;
      if(array[mid]==target){
        return mid;
      }else if(array[mid]>target){
        //the right element may be the result
        //cannot be ruled out
        right=mid;
      }else{
        //the left element may be the result
        //cannot be ruled out
        left=mid;
      }
    }
    //Post processing
    if(target-array[left]<array[right]-target){
      return left;
    }else{
      return right;
    }
  }

原文地址:https://www.cnblogs.com/brooksli/p/10850744.html

时间: 2024-11-09 15:13:55

Binary Search 专栏的相关文章

72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / ___2__ ___8__ / \ / 0 _4 7 9 / 3 5 例如: 2,8 -->6 2,4-–>2 原文描述: 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

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 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 v and w as the lowest node in T th

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

LeetCode 50 Pow(x, n)(Math、Binary Search)(*)

翻译 实现pow(x, n). 原文 Implement pow(x, n). 分析 首先给大家推荐维基百科: zh.wikipedia.org/wiki/二元搜尋樹 en.wikipedia.org/wiki/Binary_search_tree 其次,大家也可以看看类似的一道题: LeetCode 69 Sqrt(x)(Math.Binary Search)(*) 然而这题我还是没有解出来,看看别人的解法-- class Solution { private: double myPowHel

LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2

[LeetCode]Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Binary Search Tree Iterator

QUESTION Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time