leetcode个人题解——#31 Next Permutation
写这题时脑子比较混乱,重写了一遍wiki大佬的解法。
算法:
According to Wikipedia, a man named Narayana Pandita presented the following simple algorithm to solve this problem in the 14th century.
- Find the largest index
ksuch thatnums[k] < nums[k + 1]. If no such index exists, just reversenumsand done. - Find the largest index
l > ksuch thatnums[k] < nums[l]. - Swap
nums[k]andnums[l]. - Reverse the sub-array
nums[k + 1:]
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int max;
int min;
for(max = nums.size() - ; max >= ; max--)
if(nums[max] < nums[max+])
break;
if(max < ){
reverse(nums.begin(),nums.end());
return;
}
else
for(min = nums.size() - ; min > ; min--)
if(nums[min] > nums[max])
break;
swap(nums[min], nums[max]);
reverse(nums.begin() + max + , nums.end());
}
};
leetcode个人题解——#31 Next Permutation的更多相关文章
- <LeetCode OJ> 31. Next Permutation
31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...
- [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 & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
随机推荐
- CentOS7 更换OpenStack-queens源
根据官网的安装文档来对OpenStack搭建时碰到一个问题,安装完centos-release-openstack-queens后相当于是增加了一个OpenStack的源,但是因为这个源是在国外安装一 ...
- 全方面了解和学习PHP框架
PHP框架是什么? PHP框架提供了一个用以构建web应用的基本框架,从而简化了用PHP编写web应用程序的流程.这样不但节省开发时间,有助于建立更稳定的应用,而且减少了重复编码的开发.框架还可 ...
- day89 DjangoRsetFramework学习---restful规范,解析器组件,Postman等
DjangoRsetFramework学习---restful规范,解析器组件,Postman等 本节目录 一 预备知识 二 restful规范 三 DRF的APIView和解析 ...
- Python3 urllib 爬取 花瓣网图片
点我去我的github上看源码 **花瓣网是动态的,所以要抓包分析,,但我真的累的不行,不想写教程了,我源码里有注释
- leetcode记录-回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- ASP.NET的服务端验证(干货)
最近有项目需要使用.net的web,啥也不说,直接开始学习.net的mvc框架.感觉微软的web项目其实还是很好用的,今天和大家分享一下服务端验证的事情.其实原理就是用到了c#的特性,特性不用多说,大 ...
- 开篇~试试word写博客
word发布博客设置,mark一下 原帖地址:http://www.cnblogs.com/liuxianan/archive/2013/04/13/3018732.html word代码高亮插件:h ...
- 2016-2017-2 《Java程序设计》第3周学习总结
20155202张旭 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章:认识对象: java两个类型系统:基本类型,类类型. 定义"构造函数& ...
- 20155232 2016-2017-3 《Java程序设计》第4周学习总结
20155232 2016-2017-3 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 所谓继承就是避免多个类间重复定义共同行为. 1.重复在程序设计上就是不好 ...
- 20155301第十二周java课程程序
20155301第十二周java课程程序 内容一:在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Ar ...