其他分享
首页 > 其他分享> > 烧饼排序

烧饼排序

作者:互联网

 

 

class Solution {
public:
    vector<int> res;
    vector<int> pancakeSort(vector<int>& arr) {
        sortt(arr,arr.size());
        return res;
    }
   //每次找到当前段内最大的烧饼经过两次翻转翻转到最下面
    void sortt(vector<int> arr,int n)
    {
        if(n==1)
        {
            return;
        }
        int maxvalue=0;
        int maxIndex=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i]>maxvalue)
            {
                maxvalue=arr[i];
                maxIndex=i;
            }
        }
        reverse(arr,0,maxIndex);
        //最大的被翻转到顶部了
        res.push_back(maxIndex+1);
        reverse(arr,0,n-1);
        //最大的被翻转到底部了
        res.push_back(n);
        sortt(arr,n-1);
    }
    void reverse(vector<int> &arr ,int i,int j)
    {
        while(i<j)
        {
            int temp=arr[i];
            arr[i]=arr[j];
            arr[j]=temp;
            i++;
            j--;

        }
    }
};

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back vector<int> res; 矢量<int> res;  

标签:arr,int,res,烧饼,maxIndex,vector,排序,翻转
来源: https://www.cnblogs.com/libin123/p/15211808.html