剑指 Offer 53 - I. 在排序数组中查找数字 I

class Solution {
public:
    int search(vector<int>& nums, int target) {
        if (nums.empty()) return 0;
        
        int n = nums.size();
        int resl = 0, resr = 0;
        int l = 0, r = n - 1;
        while (l < r) {
            int mid = l + r >> 1;
            if (nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        resl = l;
        
        if (nums[l] != target) return 0;
        
        l = 0, r = n - 1;
        while (l < r) {
            int mid = l + r + 1 >> 1;
            if (nums[mid] <= target) l = mid;
            else r = mid - 1;
        }
        resr = l;
        
        return resr - resl + 1;
    }
};
上一篇:[LeetCode] #53 最大子序和


下一篇:数据可视化基础专题(53):Pandas基础(17)常用函数(二)数据清洗函数