Permutations leetcode java
题目:
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]
.
题解:
这道题就用循环递归解决子问题。
因为求所有组合,这就意味着不能重复使用元素,要用visited数组。
有因为是所有可能的组合,所以循环length次,就是这里面每位都有可能有length个可能性。
正因为如此,每一层递归就不需要传递一个start点,告诉他从哪开始(因为都是从头开始循环)。
代码如下:
1 public ArrayList<ArrayList<Integer>> permute(int[] num) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 ArrayList<Integer> item = new ArrayList<Integer>();
4
5 if(num.length==0||num==null)
6 return res;
7 boolean[] visited = new boolean[num.length];
8
9 permutation_helper(num,res,item,visited);
return res;
}
public void permutation_helper(int[] num, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> item,boolean[] visited){
if(item.size()==num.length){
res.add(new ArrayList<Integer>(item));
return;
}
for(int i = 0; i<num.length;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;
}
}
}
Permutations leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Permutations II leetcode java
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Permutations [LeetCode]
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【目录】LeetCode Java实现
这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...
随机推荐
- BZOJ2465: [中山市选2009]小球
Description 给定n个不同颜色的球,每个球都有一个分数,同时有m个瓶子,每个瓶子都有固定的容量.现在,你必须把球放到瓶子里面.请编程计算最多能放多少个球到这些瓶子里. Inpu ...
- 吴恩达-coursera-机器学习-week10
十七.大规模机器学习(Large Scale Machine Learning) 17.1 大型数据集的学习 17.2 随机梯度下降法 17.3 小批量梯度下降 17.4 随机梯度下降收敛 17.5 ...
- PHP 命名空间与自动加载机制介绍
include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 requ ...
- USB with NXP Microcontrollers
USB with NXP Microcontrollers NXP Advantages NXP's microcontroller portfolio features the latest USB ...
- 百度外卖接口调试 C#版
主类 class Program { static void Main(string[] args) { string cmdStr = &qu ...
- [Winform]无边框窗口悬浮右下角并可以拖拽移动
摘要 简单实现了一个这样的功能,程序启动时,窗口悬固定在右下角,并可以通过鼠标拖拽移动. 核心代码块 无边框窗口并不出现在任务栏 //无边框 this.FormBorderStyle = System ...
- 一步一步学习ASP.NET 5 (一)-基本概念和环境配置
编者语:时代在变,在csdn开博一年就发了那么的两篇文章.不管是什么原因都认为有愧了.可是今年重心都会在这里发表一些文章,和大家谈谈.NET, 移动跨平台,云计算等热门话题.希望有更好的交流. 好吧言 ...
- Git 的 WindowsXP安装
文章1: http://blog.sina.com.cn/s/blog_5063e4c80100sqzq.html 一.安装必要客户端 1. TortoiseGit http://tortoisegi ...
- ASP.NET Web API 中的返回数据格式以及依赖注入
本篇涉及ASP.NET Web API中的返回数据合适和依赖注入. 获取数据 public IEnumerable<Food> Get() { var results = reop.Get ...
- Android:intent的基础
只有一个活动的应用也太简单了吧?没错,你的追求应该更高一点.不管你想创建多少 个活动,方法都和上一节中介绍的是一样的.唯一的问题在于,你在启动器中点击应用的图 标只会进入到该应用的主活动,那么怎样才能 ...