34. Search for a Range

public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int []res=new int[2];
        int size=nums.length;

        int left=0;
        int right=size-1;

        int res1=-1;
        int res0=-1;

        while(left!=right&&left<=right)
        {
            int temp=nums[(left+right)/2];
            if(temp<target)
            {
                left=(left+right)/2+1;
            }
            else
            {
                right=(left+right)/2;
            }
        }

        if(nums[left]==target)
            res0=left;

        left=0;
        right=size-1;
        while(left!=right&&left<=right)
        {
            int temp=nums[(left+right+1)/2];
            if(temp>target)
            {
                right=(left+right+1)/2-1;
            }
            else
            {
                left=(left+right+1)/2;
            }

        }
        if(nums[right]==target)
            res1=right;

        res[0]=res0;
        res[1]=res1;

        return res;
    }
}
时间: 2024-12-28 21:45:01

34. Search for a Range的相关文章

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium 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(l

[LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity

[Leetcode][Python]34: Search for a Range

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 34: Search for a Rangehttps://oj.leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runt

[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,

leetCode 34.Search for a Range (搜索范围) 解题思路和方法

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 ex

LeetCode 34. Search for a Range (找到一个范围)

Given an array of integers sorted in ascending order, 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 e

LeetCode OJ 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,

Java [leetcode 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

[email&#160;protected] [34] Search for a Range (STL Binary Search)

https://leetcode.com/problems/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