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, 7, 8, 8, 10] and target value 8,
return [3, 4].
(给定一个排序过的整数数组(相等的数一定连在一起),找到起始和结束位置给定的目标值。
 你的算法的时间复杂度必须为O(log n)。
 如果目标没有在数组中被找到,返回[-1,-1]。
 例如,
 给[5,7,7,8,8,10]和目标值8,

 返回[3,4]。)

public class Search_for_a_Range_二分查找 {
	//返回的结果数组
    public static int[] searchRange(int[] A, int target) {  
        int[] res = new int[2];  
        res[0] = -1;  
        res[1] = -1;  
        if(A==null || A.length==0)  
        {  
            return res;  
        }  
        int l=0;  
        int r=A.length-1;  
        int m=(l+r)/2;
        //普通二分搜索找到数字
        while(l<=r)  
        {  
            m=(l+r)/2;  
            if(A[m]==target)  
            {  
                res[0]=m;  
                res[1]=m;  
                break;  
            }  
            else if(A[m]>target)  
            {  
                r = m-1;  
            }  
            else  
            {  
                l = m+1;  
            }  
        }
        //如果上面找不到该数字直接返回结果数组
        if(A[m]!=target)  
            return res;  
        //搜索右面
        int newL = m;  
        int newR = A.length-1;  
        while(newL<=newR)  
        {  
            int newM=(newL+newR)/2;  
            if(A[newM]==target)  
            {  
                newL = newM+1;  
            }  
            else  
            {  
                newR = newM-1;  
            }              
        }  
        res[1]=newR;
        //搜索左面
        newL = 0;  
        newR = m;  
        while(newL<=newR)  
        {  
            int newM=(newL+newR)/2;  
            if(A[newM]==target)  
            {  
                newR = newM-1;  
            }  
            else  
            {  
                newL = newM+1;  
            }              
        }  
        res[0]=newL;          
        return res;  
    }  
	public static void main(String[] args) {
		int array[]={5,7,7,7,7,7};
		int target=7;
		System.out.println(Arrays.toString(searchRange(array, target)));
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's the previous implementation of BST: ```python class Node: def __init__(self, key): self.left = None self.right = None self.val = key class BST: def __init__(self): self.root = None def insert(self, key): self.root = self._insert(self.root, key) def _insert(self, node, key): if node is None: return Node(key) else: if key < node.val: node.left = self._insert(node.left, key) else: node.right = self._insert(node.right, key) return node def search(self, key): return self._search(self.root, key) def _search(self, node, key): if node is None or node.val == key: return node elif key < node.val: return self._search(node.left, key) else: return self._search(node.right, key) ``` And here's the implementation of `range_search`: ```python def range_search(bst, r): result = [] _range_search(bst.root, r[0], r[1], result) return result def _range_search(node, min_val, max_val, result): if node is None: return if min_val <= node.val <= max_val: _range_search(node.left, min_val, max_val, result) result.append(node.val) _range_search(node.right, min_val, max_val, result) elif node.val < min_val: _range_search(node.right, min_val, max_val, result) else: _range_search(node.left, min_val, max_val, result) ``` The `range_search` function takes a BST and a range `r` as input. It initializes an empty list `result` which will contain the nodes within the range. `_range_search` is a recursive helper function. It takes a node, a minimum value `min_val`, a maximum value `max_val`, and the `result` list as input. If the node is within the range, it recursively calls itself on the left and right subtrees, adds the node's value to the result list, and then recursively calls itself on the left and right subtrees again. If the node's value is less than the minimum value of the range, it only recursively calls itself on the right subtree. If the node's value is greater than the maximum value of the range, it only recursively calls itself on the left subtree. Finally, the `range_search` function calls `_range_search` on the root node of the BST and returns the result list.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值