バイナリサーチの練習

  必ずしも整然と配列で検索されていないバイナリ検索を使用して、スクリーニングフィルタデータのうち半分後にした後の状態である限り使用することができます

書式#include <iostreamの> 
の#include < 文字列 > 
の#include <スタック> // POP、トップ、プッシュ 
する#include <ベクトル> 使用して、名前空間STD;
 クラスのソリューション
{ パブリックint型 getLessIndex(ベクトル< 整数 > ARR)
    { IF ARR(。 ())空// 1.第一の空気かどうかを決定するリターン - 。1 ;
         のiF(arr.sizeを()== 1 || ARR [ 0 ] <ARRの[ 1 ])// の配列の先頭分析2. 戻り0 ;
        
 

        
            
             IF(ARR [arr.size() - 1 ] <ARR [arr.size() - 2 ])// アレイ3の分析の終了
            戻り arr.size() - 1 ; 

        INT低= 1、高は= arr.size () - 2 、MID、
         一方(低<= 高)
        { 
            MID =低+(高-低)/ 2 ; // オーバーフローに過度に低い+高い結果を防止するために
            IF(ARR [MID]> ARR [半ば1。 ] )// MIDが最小位置決めすることができるMIDの左側に、左から減少 
                ハイ=半ば。1 ;
              IF(ARR [MID]> ARR [MID + 1 ])// 中間権利は最小位置が右半ばに表示されることから、減少した 
                低=ミッド+ 。1 ;
             他は// の中間位置を見つける
                戻り、中間を
        } 
        リターン - 1 ; 
    } 
}; 
int型のmain()
{ 
    int型 [ 30 ] = { 10510501247329546510671094372954610 }。
    ベクター < INT > ARR(+ 30 )。
    溶液S; 
    COUT << s.getLessIndex(ARR)。
    リターン 0 ; 
}

#include<iostream>
#include<string>
#include<bitset>//pop,top,push
#include<vector>
#include<cmath>
using namespace std;
class QuickPower
{
public://https://www.cnblogs.com/Knuth/archive/2009/09/04/1559949.html
    int getPower(int k, int N)
    {
        if(k==0)
            return 0;
        if(N==0)
            return 1;
        if(k>1000000007)
            k=k%1000000007;

        //arr数组中的每个值对应k的二进制bit中的每一位
        vector<long> arr;
        vector<int>  bit;
        //把k拆分为二进制操作
        long long  m=N,temp=k,res;
        while(m)
        {
            arr.push_back(temp);
            temp*=temp;
            if(temp>1000000007)
                temp=temp%1000000007;
            if(m%2)
                bit.push_back(1);
            else
                bit.push_back(0);
            m=m/2;
        }
        for(int i=0,res=1;i<bit.size();i++)
            if(bit[i])
            {
                res*=arr[i];
                if(res>1000000007)
                    res=res%1000000007;
            }
        return res%(1000000007);
    }
};
int main()
{
    QuickPower s;
    cout<<s.getPower(2,14876069);
    return 0;
}

  完全二叉树增加结点在树的最后一层从左到右依次添加,删除结点从右到左依次删除

  找根节点的右子树的最左子树出现的位置,是否和左子树中的最左子节点出现在同一层

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class CountNodes {
public:
    int count(TreeNode* root)
    {
        if(!root)
            return 0;

        int Lengthofleft=DepthofCom_Bitree((*root).left);
        int Lengthofright=DepthofCom_Bitree((*root).right);
        //深度为h的二叉树最多有2^h-1个结点(h>=1),最少有h个结点
        if(Lengthofleft==Lengthofright)
            return pow(2.0,Lengthofleft)+count((*root).right);
        else
            return pow(2.0,Lengthofright)+count((*root).left);
    }
    int DepthofCom_Bitree(TreeNode *root)
    {
        if(!root)
            return 0;
        TreeNode *p=root;
        int len=0;
        while(p)
        {
            len++;
            p=(*p).left;
        }
        return len;
    }
};

#include<iostream>
#include<vector>
using namespace std;
class MinValue {
public:
    int getMin(vector<int> arr, int n)
    {
        if(arr.empty())
            exit(-1);
        if(n==1||arr[0]<arr[n-1])//整个数组是有序的
            return arr[0];

        int low=0,high=n-1,mid;
        while(low<high)
        {
            mid=low+(high-low)/2;
            if(arr[low]>arr[mid])//(升序数组)右半部分肯定移动到数组开头且最小值位于中间元素之前 6 5 1 2 3 4
                high=mid;
            else if(arr[mid]>arr[high])//最小值位于中间元素之后 4 5 6 1 2 3
                low=mid+1;
            else//arr[low]==arr[mid]==arr[high]  2 1 2 2 2 2
                break;
        }
        if(low==high)
            return arr[low];

        int min=arr[low];
        while(low<=high)
        {
            if(arr[low]<min)
                min=arr[low];
            low++;
        }
        return min;
    }
};
int main()
{
    int a[6]={4,5,6,1,2,3};
    vector<int> arr(a,a+6);
    MinValue s;
    cout<<s.getMin(arr,6);
    return 0;
}

#include<iostream>
#include<vector>
using namespace std;
class LeftMostAppearance 
{
public:
    int findPos(vector<int> arr, int n, int num) 
    {
        if(!n)
            return -1;
            
        int low=0,high=n-1,mid;
        int res=-1;
        while(low<=high)
        {
            mid=low+(high-low)/2;
            if(arr[mid]<num)
                low=mid+1;
            else if(arr[mid]>num)
                high=mid-1;
            else
            {
                res=mid;
                high=mid-1;
            }
        }
        return res;
    }
};
int main()
{
    int a[5]={1,2,3,3,4};
    vector<int> arr(a,a+5);
    LeftMostAppearance s;
    cout<<s.findPos(arr,5,3);
    return 0;
}

#include<iostream>
#include<vector>
using namespace std;
class Find {
public:
    int findPos(vector<int> arr, int n) 
    {
        if(arr.empty()||arr[0]>n-1||arr[n-1]<0)
            return -1;
            
        int res=-1;
        int low=0,high=n-1,mid=0;
        while(low<=high)
        {
            mid=low+(high-low)/2;
            if(arr[mid]<mid)//因为数组有序,所以左边的值都小于他的下标
                low=mid+1;
            else if(arr[mid]>mid)//右边得值肯定也都大于他的下标
                high=mid-1;
            else
            {
                res=mid;
                high=mid-1;
            }
        }
        return res;
    }
};
int main()
{
    int a[5]={-1,0,2,3};
    vector<int> arr(a,a+4);
    Find s;
    cout<<s.findPos(arr,4);
    return 0;
}

 

おすすめ

転載: www.cnblogs.com/tianzeng/p/11257167.html