{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "91c6730282074a3ba8f208b7c7574dda", "keywords": "数组,回溯", "title": "全排列", "desc": [ { "content": "\n

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

示例:

输入: [1,2,3]
输出:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
", "language": "markdown" } ], "answer": [ { "content": "", "language": "java" } ], "prepared": [ [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ] ], "template": { "content": "class Solution {\n\tpublic List> permute(int[] nums) {\n\n\t\tList> result = new ArrayList>();\n\t\tArrays.sort(nums);\n\t\tList first = new ArrayList();\n\t\tfor (int r = 0; r < nums.length; r++) {\n\t\t\tfirst.add(nums[r]);\n\t\t}\n\t\tresult.add(first);\n\t\tint i = nums.length - 2;\n\t\twhile (i >= 0) {\n\t\t\tif (nums[i] < nums[i + 1]) {\n\t\t\t\tint temp = nums[i];\n\t\t\t\tfor (int j = nums.length - 1; j > i; j--) {\n\t\t\t\t\tif (nums[j] > temp) {\n\t\t\t\t\t\tnums[i] = nums[j];\n\t\t\t\t\t\tnums[j] = temp;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tnums = quick_sort(nums, i + 1, nums.length - 1);\n\t\t\t\tList sub = new ArrayList();\n\t\t\t\tfor (int t = 0; t < nums.length; t++) {\n\t\t\t\t\tsub.add(nums[t]);\n\t\t\t\t}\n\t\t\t\tresult.add(sub);\n\t\t\t\ti = nums.length - 2;\n\n\t\t\t} else {\n\t\t\t\ti--;\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\n\t}\n\n\tpublic int[] quick_sort(int[] a, int left, int right) {\n\t\tif (left < right) {\n\t\t\tint l = left;\n\t\t\tint r = right;\n\t\t\tint temp = a[l];\n\t\t\twhile (l != r) {\n\t\t\t\twhile (l < r && a[r] > temp) {\n\t\t\t\t\tr--;\n\t\t\t\t}\n\t\t\t\tif (l < r) {\n\t\t\t\t\ta[l] = a[r];\n\t\t\t\t\tl++;\n\t\t\t\t}\n\n\t\t\t\twhile (l < r && a[l] < temp) {\n\t\t\t\t\tl++;\n\t\t\t\t}\n\t\t\t\tif (l < r) {\n\t\t\t\t\ta[r] = a[l];\n\t\t\t\t\tr--;\n\t\t\t\t}\n\n\t\t\t}\n\t\t\ta[l] = temp;\n\t\t\tquick_sort(a, left, l - 1);\n\t\t\tquick_sort(a, l + 1, right);\n\t\t}\n\t\treturn a;\n\t}\n}", "language": "java" }, "node_id": "dailycode-4a27c74c6807494493bee7bc2b275d32", "license": "csdn.net", "created_at": 1637894158, "topic_link": "https://bbs.csdn.net/topics/600469820" }