Given a collection of distinct numbers, return all possible permutations.排列组合
来源:互联网 发布:制定网络安全标准 编辑:程序博客网 时间:2024/06/06 05:07
/*
Given a collection of distinct 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].
*/
</pre><pre name="code" class="java">
import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.List;public class Permutations {public static void main(String[] args) {List<List<Integer>> a=new Permutations().permute(new int[]{5,4,6,2});for(List<Integer> b:a){for(Integer c:b)System.out.print(c);System.out.println();}} public List<List<Integer>> permute(int[] nums) { int length=nums.length; int size=0; for(int i=1;i<=length;i++)size*=i; List<List<Integer>> result=new ArrayList<List<Integer>>(size); for(int i=1;i<=length;i++) { if(result.size()==0) { List<Integer> tem=new ArrayList<Integer>(); tem.add(nums[0]); result.add(tem); continue; } int len=result.size(); //由于要新增i个已有快 for(int aa=0;aa<i-1;aa++) { for(int z=0;z<len;z++) result.add(new ArrayList<Integer>(result.get(z))); } //根据所处块位置决定所加元素位置 for(int j=0;j<result.size();j++) { if(result.get(j).size()<j/len)//无法用add在最后插入,因此要单独拿出来 { result.get(j).add(nums[i-1]); } else { result.get(j).add(j/len, nums[i-1]); } } } return result; }}
0 0
- Given a collection of distinct numbers, return all possible permutations.排列组合
- LeetCode Online Judge:Given a set of distinct integers, S, return all possible subsets.
- Generating all permutations of a given string(JAVA)
- Given an array of numbers, return array of products of all other numbers (no division)
- Write a C program to print all permutations of a given string
- Write a program to print all permutations of a given string
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- All factors of a given number
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- Write a method to compute all permutations of a string
- Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or
- [LeetCode] Permutations (Find all permutations of a integer array w/ or w/o duplicates)
- [hackerrank Value of all Permutations]
- Print all Jumping Numbers smaller than or equal to a given value
- CareerCup Output the amount of all possible strings of length N that don't of have consecutive a,b,c
- Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthe
- NYOJ - 436 - sum of all integer numbers(注意a<0)
- Permutations of a String
- 动态规划 杭电2084 数塔
- 面试笔试
- iOS深入学习(Block全面分析)
- 由Android 65K方法数限制引发的思考
- R使用图片为背景画图
- Given a collection of distinct numbers, return all possible permutations.排列组合
- hihocoder 1078 线段树的区间修改 (线段树 区间更新 模板)
- ReplatKit框架--转载
- (置顶)HDOJ题目分类
- 【Developer Log】ProGuard扰码:ClassFormatError
- shiro 授权
- JS奇技淫巧
- jdk自带webservice 应用
- jQuery validate验证隐藏表单(hidden)域