标签归档:leetcode

RSS feed of leetcode

[LeetCode]Permutations

题目描述:

LeetCode 46. Permutations

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

题目大意:

给定一个唯一数字的集合,返回所有可能的排列。

测试用例如题目描述。

解题思路:

递归(Recursion)

记传入数组为nums,若nums的长度不大于1,则直接返回[nums]

遍历nums,从中抽取一个数num,递归计算剩余数字组成的数组n,然后将num与结果合并

Python代码:

class Solution(object):
    def ...

继续阅读

[LeetCode]Next Permutation

题目描述:

LeetCode 31. 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 ...

继续阅读