[Lintcode]52. Next Permutation

52. Next Permutation

  • 本题难度: Medium
  • Topic: Greedy

Description

52. Next Permutation

  • 本题难度: Medium
  • Topic: Greedy

Description

Given a list of integers, which denote a permutation.

Find the next permutation in ascending order.

Example

Example 1:

Input:[1]

Output:[1]

Example 2:

Input:[1,3,2,3]

Output:[1,3,3,2]

Example 3:

Input:[4,3,2,1]

Output:[1,2,3,4]

Notice

The list may contains duplicate integers.

别人的代码

class Solution:
    """
    @param nums: A list of integers
    @return: A list of integers
    """

    def nextPermutation(self, num):
        # write your code here
        for i in range(len(num)-2, -1, -1):
            if num[i] < num[i+1]:
                break
        else:
            num.reverse()
            return num
        for j in range(len(num)-1, i, -1):
            if num[j] > num[i]:
                num[i], num[j] = num[j], num[i]
                break
        for j in range(0, (len(num) - i)//2):
            num[i+j+1], num[len(num)-j-1] = num[len(num)-j-1], num[i+j+1]
        return num

思路

写到怀疑人生。

一开始就没好好注意条件活该!

原文地址:https://www.cnblogs.com/siriusli/p/10360718.html

时间: 2024-10-15 08:47:00

[Lintcode]52. Next Permutation的相关文章

[leetcode 52] Next Permutation Report

Question: Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Example For [1,3,2,3], the next permutation is [1,3,3,2] For [4,3,2,1], the next permutation is [1,2,3,4] Note The list may contains duplica

输出字符串的所有排列

题目:终端随机输入一串字符串,输出该字符串的所有排列. 例如,输入:"abc",输出:abc.acb.bac.bca.cab.cba 递归解决: 关键在于递归条件.边界条件(start==end).去重(例如"aa",可以借助HashSet进行去重) 关键词:permutation [p??mj?'te??(?)n] 排列,置换 recursion [r?'k???(?)n]  递归,循环 具体代码参看如下: 1 import java.util.ArrayList

Yandex Algorithm 2018 Qualification

A. Lottery code: 1 /* 2 by skydog 3 */ 4 #include <iostream> 5 #include <cstdio> 6 #include <vector> 7 #include <utility> 8 #include <algorithm> 9 #include <cmath> 10 #include <cstring> 11 #include <map> 12

lintcode 容易题:Permutation Index 排列序号

题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有求出所有的排列,然后找出其对应的下标,但是怎么求出排列,在做Project Euler 时候碰到过,但是现在我又不会写了,那时候毕竟是抄别人的程序的.在geekviewpoint看到一种很厉害的解法,不需要求所有的排列,直接根据给的数组进行求解. 思路: 1.对于四位数:4213 = 4*100+2

Lintcode: Permutation Index

Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1. 在计算最终的 index 时需要动态计算某个数的相对大小.我们可通过两重循环得出到某个索引处值的相对大小. 正确 以4,1,2为例,4为第3大

LintCode &quot;Permutation Index II&quot; !

Simply a variation to "Permutation Index". When calculating current digit index, we consider duplicated case. Again, similar as "Digit Counts", it is another counting problem and stil digit by digit. And please note: we can use Fenwick

lintcode 中等题:next permutation下一个排列

题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] 注意 排列中可能包含重复的整数 解题 和上一题求上一个排列应该很类似 1.对这个数,先从右到左找到递增序列的前一个位置,peakInd 2.若peakInd = -1 这个数直接逆序就是答案了 3.peakInd>= 0 peakInd这个位置的所,和 peakInd 到nums.size() -1

LintCode Problems Link Table

Practice Makes Perfect! # Problem Link Tag Difficulty 1 A + B problem Bitwise Operation Easy 2 Trailing Zeros Math Easy 3 Digit Counts   Medium 4 Ugly Number II   Medium 5 Kth Largest Element   Medium 6 Merge Two Sorted Arrays   Easy 7 Binary Tree Se

CF 500 B. New Year Permutation 并查集

User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible. Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k