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题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
随机推荐
- MySQL->AUTO_INCREMENT[20180516]
MySQL表格中自增长主键AUTO_INCREMENT使用,实现序列的最简单的方式 创建一个AUTO_INCREMENT自增的表 mysql> create table seq_test( ...
- 误卸载glibc类库导致系统崩溃解决方案
由于系统中没有yum环境需要编译安装redis,但系统中却没有安装gcc和gcc-c++,挂载本地镜像安装gcc和gcc-c++由于版本太高,由于一时疏忽误将系统的依赖库glibc使用rpm -e 命 ...
- 日期插件rolldate.js的使用
日期插件rolldate.js的使用 下载地址:http://www.jq22.com/jquery-info19834 效果: 代码: <!DOCTYPE html> <html ...
- 四、spring成长之路——springIOC容器(下)
目录 5.spring注解开发(Spring扩展知识) 5.1定义配置类:@Configuration 声明一个类为IOC容器 @Bean定义一个Bean 5.2.按照条件进行注入 5.3.@Impo ...
- VS2015创建WDK的问题
在微软官网找了半天.. 搜索window driver kit,好吧.进入一页英文页面.. https://docs.microsoft.com/en-us/windows-hardware/driv ...
- 用go实现简单的冒泡排序
package main import "fmt" func main(){ var arr = [] int { 9 , 6 , 2 , 5 , 8 , 10 , 12 , 1 ...
- golang基础--Interface接口
接口是一个或多个方法签名名的集合,定义方式如下 type Interface_Name interface { method_a() string method_b() int .... } 只要某个 ...
- java虚拟机内存管理
1. java虚拟机内存如下 2. 运行时数据区 内存图分析:
- Nexus Repository3安装和maven,npm配置(Linux)
Nexus Repository下载 根据操作系统选择指定版本,本文针对Linux安装,其他的安装过程可能有所差异. https://help.sonatype.com/repomanager3/do ...
- 20155315 2016-2017-2 《Java程序设计》第四周学习总结
教材学习内容总结 1.继承与多态 Java中只有单一继承,也就是只能有一个父类; 多态即指一个父类可由多个子类继承. 继承可以复用代码,更大的用处是实现「多态」. 封装是继承的基础,继承是多态的基础 ...