LeetCode Permutations
来源:互联网 发布:java websocket 跨域 编辑:程序博客网 时间:2024/06/06 08:47
Description:
Given a collection of numbers, return all possible permutations.
Solution:
I remember this is the most basic algorithm on the book Data Structure.
import java.util.ArrayList;import java.util.List;public class Solution {List<List<Integer>> list;public List<List<Integer>> permute(int[] nums) {list = new ArrayList<List<Integer>>();dfs(0, nums);return list;}void dfs(int tot, int nums[]) {if (tot == nums.length) {ArrayList<Integer> l = new ArrayList<Integer>();for (int i = 0; i < nums.length; i++)l.add(nums[i]);System.out.println(l);list.add(l);return;}int temp;for (int i = tot; i < nums.length; i++) {temp = nums[tot];nums[tot] = nums[i];nums[i] = temp;dfs(tot + 1, nums);temp = nums[tot];nums[tot] = nums[i];nums[i] = temp;}}public static void main(String[] args) {Solution s = new Solution();s.permute(new int[] { 1, 2, 3, 4 });}}
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
- 十分钟搞定pandas
- 题目记录
- Nginx日志配置
- 数据库SQL优化大总结
- iconfont取代imageView来展示图标
- LeetCode Permutations
- 线索二叉树(Threaded Binary Tree)
- LNMP编译安装
- 高通Android平台硬件调试之Camera篇
- JavaScript权威指南_111_第15章_脚本化文档_15.1-DOM概览
- Nginx的rewrite应用
- JQ动画效果
- 程序员老矣,尚能编否?
- Linux cp 命令