国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

LeetCode 31. Next Permutation

2019-11-08 03:12:36
字體:
來源:轉載
供稿:網友

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,23,2,1 → 1,2,3

1,1,5 → 1,5,1

answer:

class Solution {public:    void nextPermutation(vector<int>& nums) {        int PRe,end;        end = nums.size() - 1;        if(end == 0) return;        pre = end - 1;        int temp;        bool find = false;        while(pre >= 0){            if(nums[pre] < nums[end]){                int index = nums.size() - 1;                while(index > pre && nums[index] <= nums[pre] ){                    index --;                }                temp = nums[pre];                nums[pre] = nums[index];                nums[index] = temp;                sort(nums.begin() + pre + 1,nums.end());                find = true;               break;            }            pre --;            end --;        }        if(!find) sort(nums.begin(),nums.end());        return ;    }};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 交城县| 开封市| 苏尼特右旗| 新泰市| 福清市| 新郑市| 丁青县| 垣曲县| 海原县| 称多县| 建水县| 德保县| 宁国市| 乳源| 长武县| 嘉峪关市| 孟津县| 青神县| 阿拉善盟| 三门峡市| 西盟| 铜鼓县| 闽侯县| 乌兰县| 弋阳县| 肃宁县| 全南县| 区。| 凤山市| 抚顺县| 兴隆县| 辽宁省| 河西区| 托克逊县| 从化市| 崇仁县| 项城市| 湘西| 兰坪| 香港| 花莲县|