【Leetcode】Permutations
来源:互联网 发布:淘宝怎么赚佣金 编辑:程序博客网 时间:2024/05/29 17:07
题目链接:https://leetcode.com/problems/permutations/
题目:
Given a collection of 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]
, and [3,2,1]
.
o see which companies asked this question
思路:
数组下标的三皇后问题,实质是全排列问题。唯一不同 都是,本题全排列的是数组的下标。只要做个对应转化就好了。
算法:
List<List<Integer>> iLists = new ArrayList<List<Integer>>();public List<List<Integer>> permute(int[] nums) {this.n = nums.length;this.cl = new int[n];this.n2 = nums;dspPermute(0);return iLists;}int[] n2;int cl[] = null;int n = 0;void dspPermute(int cur) {if (cur == n) {List<Integer> list = new ArrayList<Integer>();for (int i = 0; i < cl.length; i++) {list.add(cl[i]);}iLists.add(list);} else {for (int i = 0; i < n; i++) { // 探索每一列cl[cur] = n2[i];// 选择n[i]为第cur个数boolean flag = true;for (int j = 0; j < cur; j++) {// cur前所有行 若无冲突if (cl[cur] == cl[j])flag = false;}if (flag == true) {dspPermute(cur + 1);}}}}
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- [LeetCode] Permutations
- Leetcode: Permutations
- [Leetcode] Permutations
- [Leetcode] Permutations
- [LeetCode]Permutations
- LeetCode-Permutations
- [leetcode] permutations
- LeetCode - Permutations
- Leetcode: Permutations
- 【leetcode】Permutations
- 【LeetCode】Permutations
- java 日期格式化
- 10003---理解MySQL——复制(Replication)
- Deep Learning Face Attributes in the Wild
- org.springframework.context.ApplicationContextAware
- 安卓自带的屏幕录制命令
- 【Leetcode】Permutations
- winform Treeview Checkbox选择事件
- python如何得到13位时间戳?
- 自定义dialog使用场景—等待层
- ARM汇编指令
- location.href和location.replace和location.reload的不同(location.replace不记录历史)
- ucloud UDataArk 测试
- 中国剩余定理入门
- LeetCode 111 Minimum Depth of Binary Tree