首页 > 编程语言 > 详细

[LeetCode][JavaScript]permute

时间:2015-11-22 23:06:53      阅读:256      评论:0      收藏:0      [点我收藏+]

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].

https://leetcode.com/problems/permutations/

 

 


 

 

数字排列。

循环每一位数字,每个数都和所有数交换(包括自己)。

 

 1 /**
 2  * @param {number[]} nums
 3  * @return {number[][]}
 4  */
 5 var permute = function(nums) {
 6     var result = [];
 7     getPermute(0);
 8     return result;
 9 
10     function getPermute(index){
11         if(index === nums.length){
12             result.push(nums.slice(0));
13             return;
14         }
15         for(var i = index; i < nums.length; i++){
16             switchNum(i, index);
17             getPermute(index + 1);
18             switchNum(i, index);
19         }
20     }
21     function switchNum(i, j){
22         var tmp = nums[i];
23         nums[i] = nums[j];
24         nums[j] = tmp;
25     }
26 };

 

 

[LeetCode][JavaScript]permute

原文:http://www.cnblogs.com/Liok3187/p/4986918.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!