binary search Template

Binary search is a famous question in algorithm. For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity. If the target number does not exist in the array, return -1. Example If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.

public class BinarySearch {
    public int binarySearch(int[] A, int target) {
        if (A.length == 0)
            return -1;
        int start = 0;
        int end = A.length - 1;
        int mid;

        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (A[mid] == target)
                end = mid;
            else if (A[mid] < target)
                start = mid;
            else if (A[mid] > target)
                end = mid;
        }

        if (A[start] == target)
            return start;
        if (A[end] == target)
            return end;
        return -1;
    }
}
时间: 2024-10-28 02:36:58

binary search Template的相关文章

[LC] Binary Search 题目/方法总结

Binary search 是最简单也十分常用的一种算法,能把O(n) 的搜索降到O(logn)这个帖子的总结是参考大神Grandyang的,甚至很多地方是直接抄袭过来,原贴在这里http://www.cnblogs.com/grandyang/p/6854825.html Binary Search Template 二分法常见关键词:sorted: 二分常见痛点有以下几个: 循环条件:start + 1 < end mid = start + (end - start) /2; A[mid]

UVA1264 &amp;&amp; UVAlive4847 Binary Search Tree

A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties: (1) Every node has a key, and no two nodes have the same key. (2) The keys in a nonempty left subtree must be smaller than the key in t

Binary Search

Time complexity O(log(n)). When the question requires O(log(n)) time complexity, we always need to think whether it can be solved by binary search. For binary search, there are several key elements to consider: 1. when to stop while loop? 2. how to c

泛型Binary Search Tree实现,And和STL map比较的经营业绩

问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常随机时候,性能会接近O(N).N是树中节点数目; 3.理想状态下.时间复杂度是O(lgN), N是树中节点的数目: 4.以下给出一个简单的实现,并比較其和STL map的性能.一样的操作,大约耗时为STL map 的2/3. 代码例如以下: #ifndef _BINARY_SEARCH_TREE_H

Binary Search汇总

Binary Search:二分查找 实现方式:recursive or iterative 注意问题,终止循环条件,移动start,end时边界值,start = mid,end = mid Template: 1. 在排序的数组中找A[i] == i的index,有重复元素存在. (cc150 Question 9.3) 因为有重复元素,所以例如: A[mid] < mid, 不仅要search right,左边也可能出现A[i] == i 所以两边都需要Search,但是可以排除一些点 L

二叉查找树(binary search tree)

二叉查找树的性质:对于树中的每个节点x,它的左子树中所有项的值不大于x的值,它的右子树中所有项的值不小于x的值. 二叉查找树应具有以下操作 ① 寻找最小项 FIND_MIN ② 寻找最大项 FIND_MAX ③ 是否包含 CONTAINS ④ 树是否为空 IS_EMPTY ⑤ 清空树 MAKE_EMPTY ⑥ 插入节点 INSERT ⑦ 移除节点 REMOVE ⑧ 打印树 PRINT_TREE (中序遍历) ⑨ 深拷贝 DEEP_CLONE 二叉查找树(binary search tree)的抽

泛型的Binary Search Tree的实现,并与STL map进行操作性能上的比较

问题描述: 1.binary search tree是一种排序二叉树.对于key值,当前节点的小于左孩子的大于右孩子的: 2.binary search tree不是自平衡树.所以,当插入数据不是很随机时候,性能会接近O(N),N是树中节点数目; 3.理想状态下,时间复杂度是O(lgN), N是树中节点的数目: 4.下面给出一个简单的实现,并比较其和STL map的性能,一样的操作,大约耗时为STL map 的2/3: 代码如下: #ifndef _BINARY_SEARCH_TREE_H_ #

[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