Permutation問題

46.Permutations
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

代碼如下:

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> rec;
        if(nums.size()<=0)
          return rec;
        dfs(nums,0,rec);
        return rec;
    }
    
    void dfs(vector<int>& nums,int start,vector<vector<int>>& rec)
    {
        if(start==nums.size())
        {
            rec.push_back(nums);
        }
        else
        {
            for(int i=start;i<nums.size();i++)
            {
                swap(nums[start],nums[i]);
                dfs(nums,start+1,rec);
                swap(nums[start],nums[i]);
            }
        }
    }
};

**47.Permutations II **
Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:

[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

代碼如下:

class Solution {
public:
    set<vector<int>> mSet;
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> rec;
        if(nums.size()<=0)
          return rec;
        sort(nums.begin(),nums.end());
        dfs(nums,0);
        for(auto a:mSet)
          rec.push_back(a);
        //return vector<vector<int>>(mSet.begin(),mSet.end());
        return rec;
    }
    
    void dfs(vector<int>& nums,int start)
    {
        if(start==nums.size())
        {
            mSet.insert(nums);
            //rec.push_back(nums);
        }
        else
        {
            for(int i=start;i<nums.size();i++)
            {
                //if(i!=start&&nums[i]==nums[i-1])
                //  continue;
                swap(nums[start],nums[i]);
                dfs(nums,start+1);
                swap(nums[start],nums[i]);
            }
        }
    }
};

**31.Next Permutation **
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

代碼如下:

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int n = nums.size();
        if(n<=0)
          return;
        int pivot = -1;  
        int i = n-1;
        for(;i>=1;i--)   //從右到左,先找出數(shù)組中非增序序列乳规,找到這個序列最大值左邊的一個數(shù)A
        {                //并找出該非遞增序列中比A大的最少的值B形葬,交換兩個數(shù)的值,然后對該遞增序列排序
            if(nums[i]>nums[i-1])
            {
                pivot = i-1;
                break;
            }
        }
        if(pivot>=0)
        for(int j=n-1;j>=0;j--)
        {
            if(nums[j]>nums[pivot])
            {
                int temp = nums[j];
                nums[j] = nums[pivot];
                nums[pivot] = temp;
                sort(nums.begin()+pivot+1,nums.end());  //牢牢記住特殊的規(guī)則
                return;
            }
            
        }
        else
        {
            sort(nums.begin(),nums.end());
        }
        return;
    }
};

**60. Permutation Sequence **
The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

"123"
"132"
"213"
"231"
"312"
"321"

Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
代碼如下:

class Solution {
public:
    string getPermutation(int n, int k) {
        string rec = "";
        vector<int> num(10,0);
        for(int i=0;i<n;i++)
        {
            num[i] = i + 1;
        }
        
        k--;
        for(int i=n;i>=1;i--)
        {
            int j = k/getFac(i-1); 
            k %= getFac(i-1);       //除去非目標數(shù)字為首的字符串所占的位數(shù)
            rec += to_string(num[j]);
            for(int k=j;k<n-1;k++)
            {
                num[k] = num[k+1];  //把可選數(shù)字左移
            }
        }
        return rec;
        
    }
    int getFac(int t)
    {
        int result=1;
        while(t>0)
        {
            result *= t;
            t--;
        }
        return result;
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末暮的,一起剝皮案震驚了整個濱河市笙以,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌冻辩,老刑警劉巖猖腕,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異恨闪,居然都是意外死亡倘感,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門凛剥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來侠仇,“玉大人,你說我怎么就攤上這事犁珠。” “怎么了互亮?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵犁享,是天一觀的道長。 經(jīng)常有香客問我豹休,道長炊昆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮凤巨,結(jié)果婚禮上视乐,老公的妹妹穿的比我還像新娘。我一直安慰自己敢茁,他們只是感情好佑淀,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著彰檬,像睡著了一般伸刃。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上逢倍,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天捧颅,我揣著相機與錄音,去河邊找鬼较雕。 笑死碉哑,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的亮蒋。 我是一名探鬼主播谭梗,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼宛蚓!你這毒婦竟也來了激捏?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤凄吏,失蹤者是張志新(化名)和其女友劉穎远舅,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體痕钢,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡图柏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了任连。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蚤吹。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖随抠,靈堂內(nèi)的尸體忽然破棺而出裁着,到底是詐尸還是另有隱情,我是刑警寧澤拱她,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布二驰,位于F島的核電站,受9級特大地震影響秉沼,放射性物質(zhì)發(fā)生泄漏桶雀。R本人自食惡果不足惜矿酵,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望矗积。 院中可真熱鬧全肮,春花似錦、人聲如沸棘捣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽柱锹。三九已至哪自,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間禁熏,已是汗流浹背壤巷。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瞧毙,地道東北人胧华。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像宙彪,于是被迫代替她去往敵國和親矩动。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容