zoukankan      html  css  js  c++  java
  • 剑指offer 38 数字在排序数组中出现的次数

    自己的写法

    class Solution {
    public:
        int GetNumberOfK(vector<int> data ,int k) {
            int length = data.size();
            if(length <= 0)
                return 0;
            for(int i = 0;i < length;i++){
                
            }
            int index1 = GetFirst(data,k,0,length-1);
            int index2 = GetLast(data,k,0,length-1);
            return exit(data,k) ? (index2-index1+1) : 0;       
        }
        int GetFirst(vector<int> data,int k,int start,int end){
            if(start == end)
                return start;
            int first = 0;
            int mid = (start+end)/2;
            if(data[mid] == k){
                if(data[mid-1] != k)
                    return mid;
                else
                    first = GetFirst(data,k,start,mid-1);    
            }
            else if(data[mid] < k)
                first = GetFirst(data,k,mid+1,end);
            else
                first = GetFirst(data,k,start,mid-1);
            return first;
        }
        int GetLast(vector<int> data,int k,int start,int end){
            if(start == end)
                return start;
            int last = end;
            int mid = (start+end)/2;
            if(data[mid] == k){
                if(data[mid+1] != k)
                    return mid;
                else
                    last = GetLast(data,k,mid+1,end);    
            }
            else if(data[mid] < k)
                last = GetLast(data,k,mid+1,end);
            else
                last = GetLast(data,k,start,mid-1);
            return last;
        }
        bool exit(vector<int> data,int k){
            int length = data.size();
            int i = 0;
            for(;i < length;i++){
                if(data[i] == k)
                    return true;
            }
            return false;
        }
    };

    更简洁的代码

    class Solution {
    public:
        int GetNumberOfK(vector<int> data ,int k) {
            if(data.empty() || k <= 0)
                return 0;
            int FirstK = GetFirstK(data,k,0,data.size()-1);
            int LastK = GetLastK(data,k,0,data.size()-1);
            if(FirstK > -1 && LastK > -1)
                return LastK - FirstK + 1;
            else
                return 0;
        }
        int GetFirstK(vector<int> data,int k,int start,int end){
            if(start > end)
                return -1;
            int mid = (end + start)/2;
            if(data[mid] == k){
                if(data[mid-1] == k)
                    end = mid - 1;
                else
                    return mid;
            }
            else if(data[mid] > k)
                end = mid - 1;
            else if(data[mid] < k)
                start = mid + 1;
            return GetFirstK(data,k,start,end);
        }
        int GetLastK(vector<int> data,int k,int start,int end){
            if(start > end)
                return -1;
            int mid = (end + start)/2;
            if(data[mid] == k){
                if(data[mid+1] == k)
                    start = mid + 1;
                else
                    return mid;
            }
            else if(data[mid] > k)
                end = mid - 1;
            else if(data[mid] < k)
                start = mid + 1;
            return GetLastK(data,k,start,end);
            
        }
    };

     用循环的方法做:

    注意一个问题,end只能等于mid - 1,不能等于mid,同样begin只能等于mid + 1,不能等于mid。如果换成mid,当只有两个数的时候,会陷入死循环!

    class Solution {
    public:
        int GetNumberOfK(vector<int> data ,int k) {
            int length = data.size();
            if(length <= 0)
                return 0;
            int begin = FindFirstK(data,k);
            int end = FindLastK(data,k);
            if(begin != -1 && end != -1 )
                return end - begin + 1;
            else
                return 0;
        }
        int FindFirstK(vector<int> data,int k){
            int length = data.size();
            int begin = 0;
            int end = length-1;
            while(begin < end){
                int mid = (begin + end)/2;
                if(data[mid] == k && data[mid-1] != k)
                    return mid;
                else if(data[mid] == k && data[mid-1] == k)
                    end = mid - 1;
                else if(data[mid] > k)
                    end = mid - 1;
                else if(data[mid] < k)
                    begin = mid + 1;
            }
            if(data[begin] == k)
                return begin;
            else
                return -1;
        }
        int FindLastK(vector<int> data,int k){
            int length = data.size();
            int begin = 0;
            int end = length-1;
            while(begin < end){
                int mid = (begin + end)/2;
                if(data[mid] == k && data[mid+1] != k)
                    return mid;
                else if(data[mid] == k && data[mid+1] == k)
                    begin = mid + 1;
                else if(data[mid] > k)
                    end = mid - 1;
                else
                    begin = mid + 1;
            }
            if(data[begin] == k)
                return begin;
            else
                return -1;
        }
    };
  • 相关阅读:
    怎么把自己电脑上的文件传到服务器本地上
    查看hive中某个表中的数据、表结构及所在路径
    python2.7读汉字的时候出现乱码,如何解决
    如何连接服务器客户端
    java常用问题排查工具
    netty源码分析之一:server的启动
    java AQS 一:
    netty源码分析之二:accept请求
    java Resource
    二:基础概述netty
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/6895159.html
Copyright © 2011-2022 走看看