写这题时脑子比较混乱,重写了一遍wiki大佬的解法。

算法:

According to Wikipedia, a man named Narayana Pandita presented the following simple algorithm to solve this problem in the 14th century.

  1. Find the largest index k such that nums[k] < nums[k + 1]. If no such index exists, just reverse nums and done.
  2. Find the largest index l > k such that nums[k] < nums[l].
  3. Swap nums[k] and nums[l].
  4. 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的更多相关文章

  1. &lt;LeetCode OJ&gt; 31. Next Permutation

    31. Next Permutation Total Accepted: 54346 Total Submissions: 212155 Difficulty: Medium Implement ne ...

  2. [Leetcode][Python]31: Next Permutation

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...

  3. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  4. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  5. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  6. leetcode 31. Next Permutation (下一个排列,模拟,二分查找)

    题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. Leetcode 简略题解 - 共567题

    Leetcode 简略题解 - 共567题     写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...

随机推荐

  1. 黑少微服务商店之Iron Cloud微服务开发云

    近日,由黑少微服务研发团队推出的Iron Cloud微服务开发云已经正式对外提供服务,这是国内第一家基于云端操作的微服务专业开发工具. Iron Cloud 微服务开发云(www.ironz.com) ...

  2. Lucene补充

    1. 课程计划 Lucene的Field Lucene的索引库维护 lucene的查询 a) Query子对象 b) QueryParser 4.Lucene相关度排序(了解) 2. Field域 2 ...

  3. 使用Hexo + GitHub Pages 搭建个人博客

    一.前言 之前是在CSDN上写博客的,但是无奈其广告满天飞,还有因为个人不太喜欢CSDN博客里的一些东西,加上看到很多技术大牛都有自己的个人博客,于是乎!便想着搭建一个自己的个人博客.其实之前写博客还 ...

  4. 关于uip中的log和打印

    简单易用,好区分 void uip_log(char *m){ printf("uIP log message: %s\n", m);}

  5. IOTutility 一个轻量级的 IOT 基础操作库

    IOTutility 一个轻量级的 IOT 基础操作库 Base utility for IOT devices, networking, controls etc... IOTutility 的目的 ...

  6. MySQL用全库备份数据恢复单表数据

    备份数据库时,采用了全库备份,但是因为某些原因需要回滚一个表的数据到备份数据库上,如果回滚整个库就比较费时间,因为可能这个表只有几十M,但是其它表可能有十几上百G,这时候就需要将需要恢复的表提取出来了 ...

  7. Go语言中其他数据与字符串类型的转换

    1 概述 Go语言是强类型语言,因此总会需要将字符串转成需要的类型.比如整型和字符串转换,字符串和布尔型的转换等.本文就介绍如何完成这些转换,以下是Go语言关于字符串转换的整理说明,主要是与切片类型的 ...

  8. 梯度下降、随机梯度下降、方差减小的梯度下降(matlab实现)

    梯度下降代码: function [ theta, J_history ] = GradinentDecent( X, y, theta, alpha, num_iter ) m = length(y ...

  9. flex Datagrid checkbox

    <?xml version="1.0" encoding="utf-8"?><!-- http://blog.flexexamples.com ...

  10. 柴柴随笔第三篇:安装虚拟机以及Linux基础入门

    虚拟机的安装 老师提供的作业指南给了我莫大的帮助,一步一步按着其中操作提示和网址链接,我首先下好了VM,也创建好了自己的第一台虚拟机. 接着按照步骤安装了Ubuntu到我的虚拟机. 到此,一切都顺风顺 ...