Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1c13488

Browse files
author
maxleisun
committedNov 10, 2022
leetcode 46 全排列-元素交换法
1 parent 0d82657 commit 1c13488

34 files changed

+47
-1
lines changed
 

‎.editorconfig

100644100755
File mode changed.

‎.gitignore

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode
2-
node_modules
2+
.DS_Store
3+
node_modules

‎1-99/46.permutation.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 全排列问题
3+
*/
4+
5+
/**
6+
* 元素交换法
7+
* @param {number[]} nums
8+
* @return {number[][]}
9+
*/
10+
exports.permuteByElementReplacement = function (nums) {
11+
if (!nums?.length) return nums;
12+
13+
const result = [];
14+
15+
function each(pre, res) {
16+
if (res.length <= 1) {
17+
result.push(pre.concat(res));
18+
return;
19+
}
20+
21+
for (let i = 0; i < res.length; i++) {
22+
const newRes = [...res];
23+
newRes.splice(i, 1, newRes[0]);
24+
each(pre.concat(res[i]), newRes.slice(1));
25+
}
26+
}
27+
28+
each([], nums);
29+
30+
return result;
31+
};

‎1-99/46.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { permuteByElementReplacement } = require("./46.permutation");
2+
3+
function runTest(method) {
4+
describe(`46. ${method.name} test`, () => {
5+
test("common case 1", () => {
6+
expect(method([1, 2, 3])).toHaveLength(6);
7+
});
8+
test("common case 2", () => {
9+
expect(method([1, 2, 3, 4])).toHaveLength(24);
10+
});
11+
});
12+
}
13+
14+
runTest(permuteByElementReplacement);

‎100-199/121.best-time-to-buy-sell-stock-i.js

100644100755
File mode changed.

‎100-199/121.best-time-to-buy-sell-stock-i.test.js

100644100755
File mode changed.

‎100-199/122.best-time-to-buy-sell-stock-ii.js

100644100755
File mode changed.

‎100-199/122.best-time-to-buy-sell-stock-ii.test.js

100644100755
File mode changed.

‎100-199/199.binary-tree-right-side-view.js

100644100755
File mode changed.

‎100-199/199.binary-tree-right-side-view.test.js

100644100755
File mode changed.

‎600-699/647.palindromic-substrings.js

100644100755
File mode changed.

‎600-699/647.palindromic-substrings.test.js

100644100755
File mode changed.

‎DP/climb-stairs.js

100644100755
File mode changed.

‎DP/climb-stairs.test.js

100644100755
File mode changed.

‎DP/coin-change.js

100644100755
File mode changed.

‎DP/max-product-subarray.js

100644100755
File mode changed.

‎DP/max-product-subarray.test.js

100644100755
File mode changed.

‎DP/triangle.js

100644100755
File mode changed.

‎DP/triangle.test.js

100644100755
File mode changed.

‎DP/word-edit-distance.js

100644100755
File mode changed.

‎README.md

100644100755
File mode changed.

‎TODO.md

100644100755
File mode changed.

‎array/longest-common-subsequence.js

100644100755
File mode changed.

‎array/longest-common-subsequence.test.js

100644100755
File mode changed.

‎array/longest-increase-subsequnce.js

100644100755
File mode changed.

‎array/longest-increase-subsequnce.test.js

100644100755
File mode changed.

‎array/max-sum-subarray.js

100644100755
File mode changed.

‎array/max-sum-subarray.test.js

100644100755
File mode changed.

‎dic/three-sum.js

100644100755
File mode changed.

‎dic/three-sum.test.js

100644100755
File mode changed.

‎helper.js

100644100755
File mode changed.

‎package.json

100644100755
File mode changed.

‎question/top-k.js

100644100755
File mode changed.

‎yarn.lock

100644100755
File mode changed.

0 commit comments

Comments
 (0)
Please sign in to comment.