题目链接

Next Permutation - LeetCode

注意点

  • 如果是字典序最大的串则要返回字典序最小的串

解法

解法一:参见:http://www.cnblogs.com/grandyang/p/4428207.html 时间复杂度O(n)。

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n = nums.size(),i = n-2,j = n-1;
while(i >= 0 && nums[i] >= nums[i+1]) i--;
cout << i;
if(i >= 0)
{
while(nums[j] <= nums[i]) j--;
swap(nums[i],nums[j]);
}
reverse(nums.begin()+i+1,nums.end());
}
};

小结

  • 排列题,有许多变种

Next Permutation - LeetCode的更多相关文章

  1. Next Permutation leetcode java

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  2. Palindrome Permutation -- LeetCode

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. Permutations II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Permutations II - LeetCode 注意点 不确定有几种排列 解法 解法一:因为有重复的数字所以排列的个数不确定几个,一直生成新的排列直 ...

  5. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  6. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  8. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据

    Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据 unity unity Editor ScirptableObject  Unity编辑器扩展 Chapt ...

  2. SQL Server 各版本安装包分享

    已将SQL Server 2005以上各版本的安装包分享到百度云盘,有需要的朋友可以下载进行安装,相关安装教程可以百度搜索.安装遇到难以解决的问题可以留言给我,2016版以上在选择功能的时候建议初学者 ...

  3. 使用yum安装文件时提示安装文件重复问题2:nodejs-10.15.3-1nodesource.x86_64: [Errno 256] No more mirrors to try.

    原因:yum命令缓存问题 解决办法: sudo yum clean all

  4. .NetCore下使用EF DbFirst操作MySql

    新建.NetCore的控制台项目 使用Nuget安装Pomelo.entityframeworkcore.mysql 工程右键--->编辑.csproj文件,把以下内容写入到工程文件 <I ...

  5. ats缓存规则

    一. 用户访问过程:1. ats收到一个用户对web对象的请求;2. 使用该地址, ats尝试着在其对象数据库(缓存)中用被请求对象的地址来定位该对象;3. 如果对象在缓存中, ats会检查该对象是否 ...

  6. 跨域Ajax -- jsonp和cors

    跨域Ajax - jsonp - cors 参考博客: http://www.cnblogs.com/wupeiqi/articles/5703697.html http://www.cnblogs. ...

  7. Dingo Api 1.0在laravel5.2中的简单应用

    Dingo Api是为基于laravel的开发提供了一系列工具集,这些工具集可以帮助开发者快速构建API.Dingo Api最新的版本是2.0.0-alpha1,这个版本需要php7.0以上的php版 ...

  8. Sprint2

    进展:主要进行了在安卓手机端进行APP开发的资料及有关学习的视频的查找等.了解也学习了这些资料还有技术.第一个任务完成了一半. 燃尽图:

  9. 简单Profibus/DP实验系统的组建

    转自:http://www.dndev.com/Profibus/profibustr/system_4.html 引言: 为了让更多刚接触到Profibus系统的朋友能对Profibus的网络架构及 ...

  10. scrapy-继承默认的user-agent 中间件

    class MyUserAgentMiddleware(UserAgentMiddleware): def __init__(self, user_agent): self.user_agent = ...