题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
http://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
JS 实现
/**
*
* @param num int整型一维数组
* @return int整型二维数组
*/
function permute( num ) {
// write code here
let res = []
let list = []
const change = (num,list)=>{
// console.log(list)
if(list.length === num.length){
// console.log(res)
// 这里要用深拷贝,不然有坑
res.push(JSON.parse(JSON.stringify(list)))
// console.log(res)
return
}
for(let i =0;i<num.length;i++){
if(list.includes(num[i])) continue
list.push(num[i])
change(num,list)
list.pop()
}
}
change(num,list)
// console.log(1,res)
return res
}
module.exports = {
permute : permute
};