国产探花免费观看_亚洲丰满少妇自慰呻吟_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 ;    }};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 重庆市| 兴安县| 荔浦县| 台安县| 原阳县| 聂荣县| 石泉县| 霍林郭勒市| 嘉黎县| 新化县| 柏乡县| 久治县| 万荣县| 德清县| 嘉鱼县| 喜德县| 旌德县| 东乡| 锦州市| 农安县| 金华市| 海城市| 黄骅市| 昆明市| 兴和县| 宜良县| 喀喇沁旗| 乐山市| 靖宇县| 铜山县| 开远市| 无锡市| 子长县| 河东区| 阿城市| 桐城市| 灵石县| 丹凤县| 霍山县| 石狮市| 富川|