1. 程式人生 > 實用技巧 >VUE,excel 流檔案上傳

VUE,excel 流檔案上傳

題目描述

統計一個數字在排序陣列中出現的次數。

示例1:

輸入: nums = [5,7,7,8,8,10], target = 8
輸出: 2

示例2:

輸入: nums = [5,7,7,8,8,10], target = 6
輸出: 0

限制:

0 <= 陣列長度 <= 50000

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof

程式碼實現

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int i, j, m;
        int left, right;
        // Search the left boundary
        i = 0; j = nums.size() - 1;
        while(i <= j) {
            m = (i + j) / 2;
            if(nums[m] < target)
                i = m + 1;
            else
                j = m - 1;
        }
        left = j;
        // Search the right boundary
        i = 0; j = nums.size() - 1;
        while(i <= j) {
            m = (i + j) / 2;
            if(nums[m] > target)
                j = m - 1;
            else
                i = m + 1;
        }
        right = i;
        return right - left - 1;
    }
};