国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode 34. Search for a Range

2019-11-08 03:08:46
字體:
來源:轉載
供稿:網友

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 example,Given [5, 7, 7, 8, 8, 10] and target value 8,

return [3, 4].

answer:

class Solution {public:    vector<int> searchRange(vector<int>& nums, int target) {        int head = -1,tail = -1,start = 0,end = nums.size() - 1;        mySearch(nums,start,end,target,head,tail);        vector<int> result;        result.push_back(head);        result.push_back(tail);        return result;    }        void mySearch(vector<int>& nums, int start, int end, int target, int & head,int & tail){        if(start > end) return;        int mid = (start + end) / 2;        if(nums[mid] < target){            mySearch(nums,mid + 1,end,target,head,tail);        }        else if(nums[mid] > target){            mySearch(nums,start,mid - 1,target,head,tail);        }        else{            int i = mid;            while(i < nums.size() && nums[i] == target) i++;            if( i > mid) tail = i - 1;            else tail = i;            i = mid;            while(i >= 0 && nums[i] == target) i--;            if(i < mid) head = i + 1;            else head = i;            return;        }    }};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴南区| 吴川市| 曲松县| 浏阳市| 白山市| 龙胜| 兴国县| 宝坻区| 郴州市| 敦煌市| 离岛区| 大化| 山丹县| 神池县| 旬阳县| 灌阳县| 百色市| 正蓝旗| 饶河县| 灯塔市| 岳阳市| 南平市| 桂平市| 墨竹工卡县| 泗洪县| 阳信县| 高平市| 锦屏县| 大足县| 新和县| 黎城县| 临沂市| 临泉县| 旬阳县| 呈贡县| 万宁市| 兴安盟| 孝昌县| 呼图壁县| 明水县| 屯昌县|