本文主要是介绍31. 下一个排列 —— LeetCode (python) [PS: LeetCode 运行环境疑似出错],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# encoding = utf-8
# 开发者:xxx
# 开发时间: 20:26
# "Stay hungry,stay foolish."class Solution(object):def nextPermutation(self, nums):import itertoolsl = len(nums)a = tuple(nums)nums.sort()permutations_lst = list(itertools.permutations(nums, l))index_u = permutations_lst.index(a)le = len(permutations_lst)if le == index_u+1:index_u = 0res1 = permutations_lst[index_u]return list(res1)res2 = permutations_lst[index_u+1]return list(res2)if __name__ == '__main__':nums = [1,2,3]sol = Solution()print(sol.nextPermutation(nums))
所用到的方法:
实现列表元素的排列组合
作者:永不止步 Python 中可以使用
例如,假设有一个列表
输出结果为:
例如,假设有一个列表
输出结果为: |
找列表中的元素位置
#列表
listA = ["o", "u" , "i"]
#获取u的索引并打印
index_u = listA.index("u")
print(index_u)
LeetCode 运行结果
我的运行结果
请各位大佬指正
这篇关于31. 下一个排列 —— LeetCode (python) [PS: LeetCode 运行环境疑似出错]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!