LeetCode 34 Search for a Range (C,C++,Java,Python)

來源:互聯網
上載者:User

標籤:c   c++   java   python   leetcode   

Problem:

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].

Solution:採用二分尋找,然後找到開頭和結尾,返回
題目大意:給定一個數組和一個目標整數,要求得出數組中目標整數的開始位置和結束位置,如果沒有輸出-1 -1
Java原始碼(407ms):
public class Solution {    public int[] searchRange(int[] nums, int target) {        int[] res = new int[2];        int l=0,len=nums.length,r=len-1,mid=0;        while(l<=r){            mid=(l+r)>>1;            if(nums[mid]==target)break;            else if(nums[mid]>target)r=mid-1;            else l=mid+1;        }        if(l<=r){            l=mid-1;            while(l>=0 && nums[l]==nums[mid])l--;            r=mid+1;            while(r<len && nums[r]==nums[mid])r++;            res[0]=l+1;            res[1]=r-1;        }else{            res[0]=-1;            res[1]=-1;        }        return res;    }}

C語言原始碼(8ms):
int* searchRange(int* nums, int numsSize, int target, int* returnSize) {    int* res=(int*)malloc(sizeof(int)*2);    int l=0,r=numsSize-1,mid;    while(l<=r){        mid=(l+r)>>1;        if(nums[mid]==target)break;        else if(nums[mid]>target)r=mid-1;        else l=mid+1;    }    if(l<=r){        l=mid-1;        while(l>=0 && nums[l]==nums[mid])l--;        r=mid+1;        while(r<numsSize && nums[r]==nums[mid])r++;        res[0]=l+1;res[1]=r-1;        *returnSize=2;        return res;    }else{        res[0]=-1;res[1]=-1;        *returnSize=2;        return res;    }}

C++原始碼(14ms):
class Solution {public:    vector<int> searchRange(vector<int>& nums, int target) {        vector<int> res;        int l=0,len=nums.size(),r=len-1,mid;        while(l<=r){            mid=(l+r)>>1;            if(nums[mid]==target)break;            else if(nums[mid]>target)r=mid-1;            else l=mid+1;        }        if(l<=r){            l=mid-1;            while(l>=0 && nums[l]==nums[mid])l--;            r=mid+1;            while(r<len && nums[r]==nums[mid])r++;            res.push_back(l+1);            res.push_back(r-1);        }else{            res.push_back(-1);            res.push_back(-1);        }        return res;    }};

Python原始碼(58ms):
class Solution:    # @param {integer[]} nums    # @param {integer} target    # @return {integer[]}    def searchRange(self, nums, target):        length=len(nums);l=0;r=length-1;mid=0        res=[-1,-1]        while l<=r:            mid=(l+r)>>1;            if nums[mid]==target:break            elif nums[mid]>target:r=mid-1            else:l=mid+1        if l<=r:            l=mid-1            while l>=0 and nums[l]==nums[mid]:l-=1            r=mid+1            while r<length and nums[r]==nums[mid]:r+=1            res[0]=l+1;res[1]=r-1        return res


LeetCode 34 Search for a Range (C,C++,Java,Python)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.