LintCode Search For a Range (Binary Search)

Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较。

循环终止条件: 最后剩两数比较(while(left + 1 < right))。

循环结束后根据要求检查最后两个数(left/ right 和 target 比较)。

public class Solution {
    /**
     *@param A : an integer sorted array
     *@param target :  an integer to be inserted
     *return : a list of length 2, [index1, index2]
     */
    public int[] searchRange(int[] A, int target) {
        int[] array = new int[2];
        array[0] = -1;
        array[1] = -1;
        if(A == null || A.length == 0) return array;

        int left = 0; int right = A.length - 1;
        while(left + 1 < right){
            int mid = (left + right) / 2;
            if(A[mid] == target){
                right = mid;
            }
            else if(A[mid] < target){
                left = mid;
            }
            else if(A[mid] > target){
                right = mid;
            }
        }
        if(A[left] == target){
            array[0] = left;
        }
        else if(A[right] == target){
            array[0] = right;
        }
        else array[0] = -1;

        left = 0; right = A.length - 1;
        while(left + 1 < right){
            int mid = (left + right) / 2;
            if(A[mid] == target){
                left = mid;
            }
            else if(A[mid] < target){
                left = mid;
            }
            else if(A[mid] > target){
                right = mid;
            }
        }
        if(A[right] == target){
            array[1] = right;
        }
        else if(A[left] == target){
            array[1] = left;
        }
        else array[1] = -1;
        return array;
    }
}
时间: 2024-11-11 18:52:45

LintCode Search For a Range (Binary Search)的相关文章

[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

96. Unique Binary Search Trees &amp;&amp; 95. Unique Binary Search Trees II &amp;&amp; 241. Different Ways to Add Parentheses

96. Unique Binary Search Trees 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. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Tree Dynamic Program

lintcode 中等题:unique Binary Search Tree 不同的二叉查找树

题目 不同的二叉查找树 给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种? 样例 给出n = 3,有5种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 解题 public class Solution { /** * @paramn n: An integer * @return: An integer */ public int numTrees(int n) { // write your code here

LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height

C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /** * @param A: A sorted (increasi

Jan 16 - Search Insert Position; Array; Binary Search; Iteration&amp;Recursion;---Iteration再补上

Recursion: 代码: public class Solution { public int searchInsert(int[] nums, int target) { int len = nums.length; if(len == 0) return 0; return findPosition(nums, 0, len-1, target); } public int findPosition(int[] nums, int start, int end, int target){

[Leetcode + Lintcode] 34. Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example,Given [5, 7,

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Example Given binary search tree as follow, after Insert node 6, the tree should be: 2 2 / \ / 1 4 --> 1 4 / / \ 3 3 6 Challenge Can you do it without recursion? /** * Definition of TreeNode: * public class TreeNod

[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree

95. Validate Binary Search Tree/98. Validate Binary Search Tree 本题难度: Easy Topic: Binary Tree Description 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 contain

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and