Lintcode: Binary Search

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.

Challenge
If the count of numbers is bigger than MAXINT, can your code work properly?

跟Leetcode里search for a range挺像的,就是找到一个target之后,还要继续找它的左边沿。最后l指针超过r指针之后, l 指针会停在左边沿上

 1 class Solution {
 2     /**
 3      * @param nums: The integer array.
 4      * @param target: Target to find.
 5      * @return: The first position of target. Position starts from 0.
 6      */
 7     public int binarySearch(int[] nums, int target) {
 8         int l = 0, r = nums.length - 1;
 9         int m = 0;
10         while (l <= r) {
11             m = (l + r) / 2;
12             if (nums[m] == target) break;
13             else if (nums[m] > target) r = m - 1;
14             else l = m + 1;
15         }
16         if (nums[m] != target) return -1;
17         l = 0;
18         r = m;
19         while (l <= r) {
20             m = (l + r) / 2;
21             if (nums[m] == target) {
22                 r = m - 1;
23             }
24             else l = m + 1;
25         }
26         return l;
27     }
28 }
时间: 2024-08-04 20:40:04

Lintcode: Binary Search的相关文章

[LintCode] Binary Search Tree Iterator

Binary Search Tree Iterator Design an iterator over a binary search tree with the following rules: Elements are visited in ascending order (i.e. an in-order traversal) next() and hasNext() queries run in O(1) time inaverage. Example For the following

[LintCode] Remove Node in Binary Search Tree

Remove Node in Binary Search Tree Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still

LintCode : Inorder Successor in Binary Search Tree

Very interesting problem! http://www.lintcode.com/zh-cn/problem/inorder-successor-in-binary-search-tree/ Description: Given a binary search tree (See Definition) and a node in it, find the in-order successor of that node in the BST. Example: Given tr

[Lintcode] Validate Binary Search Tree

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 c

【Lintcode】087.Remove Node in Binary Search Tree

题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after rem

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public

Lintcode: Search Range in Binary Search Tree

Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. Example

[lintcode easy]Convert Sorted Array to Binary Search Tree With Minimal Height

Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Example Given [1,2,3,4,5,6,7], return 4 / 2 6 / \ / 1 3 5 7 Note There may exist multiple valid solutions, return any of them. ////////////////////// 二叉查

[Lintcode] Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree. Example Given binary search tree as follow, after Insert node 6, the tree