麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

LeetCode 60. Permutation Sequence

2019-11-14 09:27:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

描述 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.

分析 簡(jiǎn)單的,可以用暴力枚舉法,調(diào)用 k ? 1 次 next_permutation()。 暴力枚舉法把前 k 個(gè)排列都求出來(lái)了,比較浪費(fèi),而我們只需要第 k 個(gè)排列。

利用康托編碼的思路,稍后細(xì)述。

代碼

class Solution {public: string getPermutation(int n, int k) { string s(n, '0'); for (int i = 0; i < n; ++i) s[i] += i + 1; for (int i = 0; i < k - 1; ++i) next_permutation(s.begin(), s.end()); return s; } template<typename BidiIt> bool next_permutation(BidiIt first, BidiIt last) { // Get a reversed range to simplify reversed traversal. const auto rfirst = reverse_iterator<BidiIt>(last); const auto rlast = reverse_iterator<BidiIt>(first); // Begin from the second last element to the first element. auto pivot = next(rfirst); // Find `pivot`, which is the first element that is no less than its // successor. `PRev` is used since `pivort` is a `reversed_iterator`. while (pivot != rlast && *pivot >= *prev(pivot)) ++pivot; // No such elemenet found, current sequence is already the largest // permutation, then rearrange to the first permutation and return false. if (pivot == rlast) { reverse(rfirst, rlast); return false; } // Scan from right to left, find the first element that is greater than // `pivot`. auto change = find_if(rfirst, pivot, bind1st(less<int>(), *pivot)); swap(*change, *pivot); reverse(rfirst, pivot); return true; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产精品久久久久影院老司 | 小视频在线看 | 激情久久免费视频 | 久久精品一区二区三区四区五区 | 97视频一二区 | 久久精品性视频 | 国产一区二区三区在线观看视频 | 天天操天天碰 | 色999国产 | 久章草影院 | 91精品动漫在线观看 | 精品一区二区电影 | 97色在线观看免费视频 | 黄色网页在线看 | 激情视频免费看 | 亚洲综合色视频在线观看 | 精品一区二区三区在线播放 | 日韩电影一区二区 | 免费亚洲视频在线观看 | 色啪综合 | 欧美日韩在线播放一区 | 国产在线精品一区二区不卡 | 亚洲一区成人在线 | 亚洲国产成人一区二区 | 中文字幕网在线 | 成人免费一区二区 | 福利在线小视频 | 久久久无码精品亚洲日韩按摩 | 国产免费一级大片 | 国产精品一区在线免费观看 | 精国产品一区二区三区四季综 | 欧美日韩1区2区 | 极品国产91在线网站 | 全视频tv | 国内一区 | 成人做爽爽爽爽免费国产软件 | 国产精品一区在线观看 | 一级一级一级一级毛片 | 久色成人网 | 中文字幕一区二区三区四区 | 成年人小视频在线观看 |