Re: [閒聊] 每日LeetCode

作者: Rushia (みけねこ的鼻屎)   2023-08-02 13:10:30
https://leetcode.com/problems/permutations/submissions/1009963724/
46. Permutations
給你一個陣列 nums,求出所有的排列。
Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3:
Input: nums = [1]
Output: [[1]]
思路:
1.昨天是組合今天是排列,dfs 所有可能的結果,如果這個元素前面已經排列過就
跳過(用一個 Set 紀錄)。
2..如果目前隊列的大小等於 nums 長度就把結果加入到結果集。
Java Code:

Links booklink

Contact Us: admin [ a t ] ucptt.com