LeetCode 第46题 全排列

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
思路:    dfs

    1) 将每个元素放到他可能存在的位置,效率较低,每次需花O(n) 时间检查该元素是否被选取过       比如:  [1 ,2 ,3]

                          root                      /      |     \                    /        |       \    下标0:         1         2        3                      / \       / \      / \    下标1:      2   3     1   3    1   2                   /     \    /     \    /     \    下标2:   3       2  3      1  2       1

    2) 通过元素之间的互相交换,效率较高,不需要检查是否被选取过
 1 class Solution46 {
 2
 3   List<List<Integer>> res = new ArrayList<>();
 4   List<Integer> list = new ArrayList<>();
 5
 6   public List<List<Integer>> permute(int[] nums) {
 7     search(0, nums);
 8     return res;
 9   }
10
11   void search(int level, int[] nums) {
12     if (level == nums.length) {
13       res.add(new ArrayList<>(list));
14     }
15     for (int num : nums) {
16       if (!list.contains(num)) {
17         list.add(num);
18         search(level + 1, nums);
19         list.remove((Integer) num);
20       }
21     }
22   }
23 }
24
25
26 ======================================
27
28
29
30 class Solution46_2 {
31
32   List<List<Integer>> res = new ArrayList<>();
33
34
35   public List<List<Integer>> permute(int[] nums) {
36     if (nums == null || nums.length == 0) {
37       return res;
38     }
39     search(0, nums);
40     return res;
41   }
42
43   private void swap(int[] nums, int pos1, int pos2) {
44     int temp = nums[pos1];
45     nums[pos1] = nums[pos2];
46     nums[pos2] = temp;
47   }
48
49   private void search(int index, int[] nums) {
50     if (index == nums.length) {
51       List<Integer> list = new ArrayList<>();
52       for (int num : nums) {
53         list.add(num);
54       }
55       res.add(list);
56     } else {
57       for (int i = index; i < nums.length; i++) {
58         swap(nums, index, i);
59         search(index + 1, nums);
60         swap(nums, index, i);
61       }
62     }
63   }
64 }


原文地址:https://www.cnblogs.com/rainbow-/p/10508908.html

时间: 2024-11-08 20:00:15

LeetCode 第46题 全排列的相关文章

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目很简单,

LeetCode第四题,Add Two Numbers

题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目非常eas