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

首頁 > 學院 > 開發設計 > 正文

LeetCode 31. Next Permutation

2019-11-14 09:00:24
字體:
來源:轉載
供稿:網友

描述 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> &num) { next_permutation(num.begin(), num.end()); } 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; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 毛片国产 | 农村少妇吞精夜夜爽视频 | 免费毛片在线 | 欧美黄色小视频 | 91精品国产91| 精品国产九九九 | 狠狠干最新网址 | 精品在线免费播放 | 国产精品一区二区手机在线观看 | 亚洲午夜久久久精品一区二区三区 | 久久国产精品99国产 | 免费黄色在线观看网站 | 精品久久久久久久久久久aⅴ | 久久av免费| 天天舔天天插 | 在线成人免费观看 | 成人做爽爽爽爽免费国产软件 | 成人一区二区三区在线 | 免费看毛片网站 | 国产成人在线视频播放 | 精品一区二区免费 | 成人三级黄色片 | 一级黄色在线免费观看 | 国内精品久久久久久影视8 嫩草影院在线观看网站成人 | 麻豆视频在线播放 | av在线免费播放 | 免费看真人a一级毛片 | 午夜精品久久久久久久99热浪潮 | 黄色毛片a级 | 色综合狠狠 | 精品国产91久久久久久浪潮蜜月 | 国产免费看片 | 精品91av | 天天夜干 | 91短视频在线播放 | 欧美乱论| 国产喷白浆10p | 看国产毛片 | 97干在线 | 午夜精品成人 | 最新一区二区三区 |