<LeetCode OJ> 31. Next Permutation
31. Next Permutation
Submissions: 212155 Difficulty: Medium
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
STL来做,秒杀
class Solution {
public:
void nextPermutation(vector<int>& nums) {
next_permutation(nums.begin(), nums.end());
}
};
此题感觉没多大意思。不好高速想出规律:
可是比較明显的规律是:
1。升序为最小组合,降序为最大组合
2,某一个数的下一个数,就是比他大的最小组合数。比方123,下一个比他大的最小组合就是132,必须从低位处理
3,stl的处理就是从低位(数组末尾)開始找起,
a)找到首个相邻的升序序列。将前一个数据与比其首次大的数交换(然后逆置后面的数)比方:123543为124533(首个升序3,5。可是4是首次比3大,则交换),此处的操作意义就是使数通过首个升序变大。
b)显然此数不是大于原数的最小数,而且后面的数逆置后才是最小的,即124533为124335。so,done!
。
//思路首先(不调用库函数):
//举例:abc,acb,bac,bca,cab,cba
class Solution {
public:
void nextPermutation(vector<int>& nums) {
if(nums.empty() || nums.size()==1)
return;
vector<int>::iterator ite1=nums.end();
ite1--;
while(true)
{
vector<int>::iterator ite2=ite1;
ite1--;//ite1始终在ite2前面一个位置(左边算作最前面)
if(*ite1 < *ite2)//升序已经出现。接着又一次从后面找首个升序位置
{
vector<int>::iterator itej=nums.end();
while(!(*ite1 < *--itej));
iter_swap(ite1,itej);//找到首个升序序列将其交换
reverse(ite2,nums.end());
return;
} if(ite1==nums.begin())
{
reverse(nums.begin(),nums.end());
return;
}
}
}
};
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50450861
原作者博客:http://blog.csdn.net/ebowtang
參考资源
【1】侯捷。《STL源代码剥析》
<LeetCode OJ> 31. Next Permutation的更多相关文章
- leetcode个人题解——#31 Next Permutation
写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- bitmap实现背景透明
近日在项目中,一直被一个问题搞得头大的很,美工要把按钮图片弄成不规则的,但是在winform里实现又不仅仅是使用简单的png图片而已.在网上找到一些方法,稍微改了一点加工成项目所需. 贴出解决方案,以 ...
- JavaScript 判断手机端操作系统(Andorid/IOS)
androidURL = "http://xxx/xxx.apk"; var browser = { versions: function() { var u = navigato ...
- DeltaFish 校园物资共享平台 第二次小组会议
软工第二周小组会议 会议地点:三教讨论区 会议时间:9:00 ~ 10:00 与会人员:软工小组成员 请假人员:刘鼎乾 整理人:艾寅中 会议记录 一.小组分工 在经过一周的调研后,组长根据调研结果和对 ...
- MSP430之section(1)
1 Intro The smallest unit of an object file is a section. A section is a block of code or data that ...
- Repeater + 分页控件 AspNetPager 研究
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...
- 微信小程序中的iPhone X适配问题
微信小程序中的iPhone X适配问题 小程序中下方的导航会被iPhone X下面的那条黑线盖住[微笑脸],所以要专门为了iPhone X做样式上的适配[微笑脸] wx.getSystemInfo({ ...
- es7 class的写法
再看vue-router源码的时候(代码链接)看到这样的代码片段: export default class VueRouter { app: any; apps: Array<any>; ...
- tomcat映射java目录 sever.xml
<Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> & ...
- 团体程序设计天梯赛-练习集-L1-042. 日期格式化
L1-042. 日期格式化 世界上不同国家有不同的写日期的习惯.比如美国人习惯写成“月-日-年”,而中国人习惯写成“年-月-日”.下面请你写个程序,自动把读入的美国格式的日期改写成中国习惯的日期. 输 ...