Skip to content

Commit

Permalink
add: 返回给定数组的所有排列
Browse files Browse the repository at this point in the history
  • Loading branch information
xldeng committed Feb 4, 2022
1 parent 95c6261 commit 9cfa834
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
17. [容器最大面积](https://github.com/dengshasha/algorithm-study/blob/master/array/containerWithMostWater.js) || [leetcode地址](https://leetcode.com/problems/container-with-most-water/submissions/)
18. [寻找数组中满足大于左右邻居的一个数](https://github.com/dengshasha/algorithm-study/blob/master/array/findPeakElement.js) || [leetcode地址](https://leetcode.com/problems/find-peak-element/)
19. [返回数组中每个元素向右到边界,比该元素小的元素的总数](https://github.com/dengshasha/algorithm-study/blob/master/array/countSmaller.js) || [leetcode地址](https://leetcode.com/problems/count-of-smaller-numbers-after-self/submissions/)
20. [返回给定数组的所有排列](https://github.com/dengshasha/algorithm-study/blob/master/array/permutations.js) || [leetcode地址](https://leetcode.com/problems/permutations/submissions/)
21. [返回给定数组(含重复元素)的所有排列](https://github.com/dengshasha/algorithm-study/blob/master/array/permutationsII.js) || [leetcode地址](https://leetcode.com/problems/permutations-ii/submissions/)

### 动态规划
1. [强盗偷东西,价值最大化](https://github.com/dengshasha/algorithm-study/blob/master/dynamicProgramming/houseRobber.js) || [leetcode地址](https://leetcode.com/problems/house-robber/)
Expand Down
29 changes: 29 additions & 0 deletions array/permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
let res = []
function recursion(l, r) {
if(l===r) {
res.push(nums.slice(0))
return;
}
for(let i = l; i <= r; i++) {
swap(i,l,nums)
recursion(l+1, r)
swap(i,l,nums)
}
}
recursion(0, nums.length-1)
return res;
};

function swap(i, j, nums) {
let temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
return nums;
}

console.log(permute([1,2,3]))
34 changes: 34 additions & 0 deletions array/permutationsII.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @param {number[]} nums
* @return {number[][]}
* leetcode address: https://leetcode.com/problems/permutations-ii/
*/
var permuteUnique = function(nums) {
let res = []
function recursion(l, r) {
if(l===r) {
res.push(nums.slice(0))
return;
}
let obj = {}
for(let i = l; i <= r; i++) {
if(!obj[nums[i]]) {
swap(i,l,nums)
recursion(l+1, r)
swap(i,l,nums)
obj[nums[i]] = 1
}
}
}
recursion(0, nums.length-1)
return res;
};

function swap(i, j, nums) {
let temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
return nums;
}

console.log(permuteUnique([2,2,1,1]))
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<title>Document</title>
</head>
<body>
<script src="string/regExpMatch.js"></script>
<script src="array/permutationsII.js"></script>
</body>
</html>

0 comments on commit 9cfa834

Please sign in to comment.