[LeetCode]题解(python):031-Next Permutation



题目来源



https://leetcode.com/problems/next-permutation/

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

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.



题意分析



Input:一个数组

Output:一个数组(原地操作)

Conditions:输出数组是输入数组的下一个比输入大的数组。 如[1, 2, 3] => [1, 3, 2]

如果输入的是最大的,那么输出最小的数组。    如[3, 2, 1]  => [1, 2, 3]



题目思路


  1. 从后往前找第一个比前面大的数字,若是不存在,则已经是最大的数组,直接原地升序排序(list.sort())返回
  2. 若是找到最大的值,记录其位置index,然后在index之后(包括index)找一个比nums[index - 1]大的最小值,并将其与nums[index - 1]交换
  3. 对index之后(包括index)的nums数组元素进行冒泡排序


AC代码(Python)



 1 _author_ = "YE"
 2 # -*- coding:utf-8 -*-
 3 class Solution(object):
 4     def nextPermutation(self, nums):
 5         """
 6         :type nums: List[int]
 7         :rtype: void Do not return anything, modify nums in-place instead.
 8         """
 9         #print(nums)
10         len1 = len(nums)
11         index = -1
12         for i in range(len1 - 1):
13             if nums[len1 - 1 - i] > nums[len1 - i - 2]:
14                 index = len1 - 1 - i
15                 break
16         if index == -1:
17             nums.sort()
18         else:
19             minindex = index
20             minvalue = nums[index]
21             for i in range(len1 - index - 1):
22                 if  minvalue > nums[index + i + 1] > nums[index - 1]:
23                     minindex = index + i + 1
24                     minvalue = nums[minindex]
25
26             nums[minindex] = nums[index - 1]
27             nums[index - 1] = minvalue
28
29             #sort other numbers from index
30             numbers = len1 - index
31             #print(numbers)
32             for i in range(numbers - 1):
33                 print(‘I:‘,i)
34                 for j in range(numbers - i - 1):
35                     if nums[len1 - j - 1] < nums[len1 - j - 2]:
36                         temp = nums[len1 - j - 1]
37                         nums[len1 - j - 1] = nums[len1 - j - 2]
38                         nums[len1 - j - 2] = temp
39
40             #print(minindex, minvalue)
41         #print(nums)



[LeetCode]题解(python):031-Next Permutation

时间: 2024-08-02 10:53:44

[LeetCode]题解(python):031-Next Permutation的相关文章

LeetCode题解之 Letter Case Permutation

1.题目描述 2.问题分析 可以使用递归的方法解决,参考了别人的答案才写出来的. 3.代码 1 vector<string> letterCasePermutation(string S) { 2 vector<string> res ; 3 recursion( S,res,0 ); 4 return res; 5 6 } 7 8 void recursion(string & s , vector<string> &r ,int p){ 9 if(

[LeetCode] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[leetcode]Candy @ Python

原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du