leetcode 46 java,leetcode46.java

package leetcode;

import java.util.ArrayList;

import java.util.List;

/**

* Created by 林剑 on 2016/10/8.

*/

public class leetcode46 {

/*public List> permute(int[] num) {

ArrayList> result = new ArrayList>();

//start from an empty list

result.add(new ArrayList());

for (int i = 0; i < num.length; i++) {

//list of list in current iteration of the array num

ArrayList> current = new ArrayList>();

for (List l : result) {

// # of locations to insert is largest index + 1

for (int j = 0; j < l.size()+1; j++) {

// + add num[i] to different locations

l.add(j, num[i]);

ArrayList temp = new ArrayList(l);

current.add(temp);

//System.out.println(temp);

// - remove num[i] add

l.remove(j);

}

}

result = new ArrayList>(current);

}

return result;

}*/

public List> permute(int[] num) {

ArrayList> res = new ArrayList>();

ArrayList item = new ArrayList();

if(num.length==0||num==null)

return res;

boolean[] visited = new boolean[num.length];

permutation_helper(num,res,item,visited);

return res;

}

public void permutation_helper(int[] num, ArrayList> res, ArrayList item,boolean[] visited){

if(item.size()==num.length){

if (!res.contains(item)){

res.add(new ArrayList(item));

return;

}

}

for(int i = 0; i

if(!visited[i]){

item.add(num[i]);

visited[i]=true;

permutation_helper(num,res,item,visited);

item.remove(item.size()-1);

visited[i]=false;

}

}

}

public static void main(String[] args) {

leetcode46 leetcode46 = new leetcode46();

int[] a = {-1,2,3};

System.out.println(leetcode46.permute(a));

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值