LeetCode

来源:互联网 发布:爱奇艺自制网络剧破案 编辑:程序博客网 时间:2024/06/02 02:41

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


在一个数组中找一个数字的开始点和结束点,查询不到返回(-1, -1)。

一个二分查找,手写二分蛮熟的了,所以试试看用函数做二分。时间复杂度O(logn),空间复杂度O(1)

class Solution {public:    vector<int> searchRange(vector<int>& nums, int target) {        vector<int> ans(2, -1);                ans[0] = lower_bound(nums.begin(), nums.end(), target) - nums.begin();        ans[1] = upper_bound(nums.begin(), nums.end(), target) - nums.begin() - 1;        if (ans[0] == nums.size() || nums[ans[0]] != target) {            ans[0] = -1;            ans[1] = -1;        }        return ans;    }};




278. First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.


一道把我写的很郁闷的二分。。。虽然他很简单。。。时间复杂度O(logn)

提交却一直TLE的原因是,平时我写的都是 mid = (le + ri) >> 1  这样可能会爆int,而 mid = le + (ri - le) / 2 就不存在这个问题。(顺便,mid = le + (ri - le) >> 1 会因为运算符优先级问题同样发生TLE情况,+优先级高于>>)

算是一个非常严重的细节了吧。

// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public:    int firstBadVersion(int n) {        int le = 1, ri = n + 1;        while (le < ri - 1) {            int mid = le + (ri - le) >> 1;               if (!isBadVersion(mid)) le = mid;            else ri = mid;        }        return isBadVersion(le) ? le : ++le;    }};



35. Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

可以写二分,也可以用lower_bound()一行解决。时间复杂度O(logn)

二分没有固定的写法,比如这次写的跟上一题就不太一样。

class Solution {public:    int searchInsert(vector<int>& nums, int target) {        int le = 0, ri = nums.size();        while (le < ri) {            int mid = le + (ri - le) / 2;            if (nums[mid] == target) return mid;            else if (nums[mid] < target) le = mid + 1;            else ri = mid;        }        return le;    }};
class Solution {public:    int searchInsert(vector<int>& nums, int target) {        return lower_bound(nums.begin(), nums.end(), target) - nums.begin();    }};


原创粉丝点击