The number of times (27) number that appears in the sort array

Topic one:The number of times the number that appears in the sort array

[A number of statistical number that appears in the sort array. The {1,2,3,3,3,3,4} and the number 3, 4 occurring due 3, the output 4]

Method One: Direct traversal

1, the analysis
began to count the number of times it appears directly through the array, because the array is already sorted, so the emergence of digital k when the array. The time complexity of this method is O ( n ) O (n) , as well as faster dichotomy.
2, the code

class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
        int len=data.size();
        if(len<=0 || k<data[0] || k>data[len-1])
            return 0;
        int count=0;
        vector<int>::const_iterator it=data.begin();
        while(it!=data.end())
        {
            if((*it)==k)
                ++count;
            it++;
        }
        return count;
    }
};
Method Two: Improved binary search

1, analysis
using a modified binary search method: k is the number to be searched

  • When the intermediate array is greater than the number k, the k on a former
  • When the intermediate array is smaller than the number k, it is located in the second half of the k
  • When the intermediate array number k is equal to, the need to continue to detect the starting position of the first half of k, the latter half of the end position k. The time complexity of this method is O ( l o g n ) O (logn)
  • The method of recursive and non-recursive two implementations

2, the code

  • Recursively
/*
* 使用递归的方式
*/
class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
        if(data.empty())
            return 0;
        int firstIndex=GetFirstIndex(data,k,0,data.size()-1);
        int lastIndex=GetLastIndex(data,k,0,data.size()-1);
        int count=0;
        if(firstIndex>-1 && lastIndex>-1)
            count=lastIndex-firstIndex+1;
        return count;
    }
    // 寻找第第一个k的下标
    int GetFirstIndex(vector<int> &data,int k,int start,int end)
    {
        if(start>end)
            return -1;
        
        int mid=start+(end-start)/2;
        if(data[mid]==k)
        {
            if((mid>start && data[mid-1]!=k) || mid==start)
                return mid;
            else
                end=mid-1;
        }
        else if(data[mid]>k)
        {
            end=mid-1;
        }
        else
            start=mid+1;
        return GetFirstIndex(data,k,start,end);
    }
    // 寻找第二个k的下标
    int GetLastIndex(vector<int> &data,int k,int start,int end)
    {
        if(start>end)
            return -1;
        
        int mid=start+(end-start)/2;
        if(data[mid]==k)
        {
            if((mid<end && data[mid+1]!=k) || mid==end)
                return mid;
            else
                start=mid+1;
        }
        else if(data[mid]>k)
        {
            end=mid-1;
        }
        else
            start=mid+1;
        return GetLastIndex(data,k,start,end);
    }
};
  • Non-recursive manner
class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
        if(data.empty())
            return 0;
        int firstIndex=GetFirstIndex(data,k,0,data.size()-1);
        int lastIndex=GetLastIndex(data,k,0,data.size()-1);
        int count=0;
        if(firstIndex>-1 && lastIndex>-1)
            count=lastIndex-firstIndex+1;
        return count;
    }
    // 寻找第第一个k的下标
    int GetFirstIndex(vector<int> &data,int k,int start,int end)
    {
        //初始中间位置,必须放在循环之外
        int mid=start+(end-start)/2; 
        // 循环条件不可漏掉 start=end 的情况
        while(start<=end)
        {
            if(data[mid]>k)
            {
                end=mid-1;
            }
            else if(data[mid]<k)
            {
                start=mid+1;
            }
            else
            {
                if((mid>start && data[mid-1]!=k)||mid==start)
                {
                    return mid;
                }
                else
                {
                    end=mid-1;
                }
            }
            mid=start+(end-start)/2; //更新中间的位置
        }
        return -1;
    }
    // 寻找第二个k的下标
    int GetLastIndex(vector<int> &data,int k,int start,int end)
    {
        int mid=start+(end-start)/2;
        while(start<=end)
        {
            if(data[mid]>k)
            {
                end=mid-1;
            }
            else if(data[mid]<k)
            {
                start=mid+1;
            }
            else
            {
                if((mid<end && data[mid+1]!=k)||mid==end)
                {
                    return mid;
                }
                else
                {
                    start=mid+1;
                }
            }
            mid=start+(end-start)/2;
        }
        return -1;
    }
};
Method 3: Use the STL container mutiple
class Solution {
public:
    int GetNumberOfK(vector<int> data ,int k) {
        if(data.empty())
            return 0;
        multiset<int> mp;
        for(auto &i:data)
        {
            mp.insert(i);
        }
        return mp.count(k);
    }
};
Published 213 original articles · won praise 48 · views 110 000 +

Guess you like

Origin blog.csdn.net/Jeffxu_lib/article/details/104869804