mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-18 09:47:56 +08:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
44
ja/codes/javascript/chapter_backtracking/permutations_ii.js
Normal file
44
ja/codes/javascript/chapter_backtracking/permutations_ii.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: permutations_ii.js
|
||||
* Created Time: 2023-05-13
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* バックトラッキング:順列 II */
|
||||
function backtrack(state, choices, selected, res) {
|
||||
// 状態の長さが要素数に等しければ、解を記録
|
||||
if (state.length === choices.length) {
|
||||
res.push([...state]);
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
const duplicated = new Set();
|
||||
choices.forEach((choice, i) => {
|
||||
// 枝刈り:要素の重複選択を許可せず、同値要素の重複選択も許可しない
|
||||
if (!selected[i] && !duplicated.has(choice)) {
|
||||
// 試行: 選択を行い、状態を更新
|
||||
duplicated.add(choice); // 選択済みの要素値を記録
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// 次の選択へ進む
|
||||
backtrack(state, choices, selected, res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
selected[i] = false;
|
||||
state.pop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* 全順列 II */
|
||||
function permutationsII(nums) {
|
||||
const res = [];
|
||||
backtrack([], nums, Array(nums.length).fill(false), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
const nums = [1, 2, 2];
|
||||
const res = permutationsII(nums);
|
||||
|
||||
console.log(`入力配列 nums = ${JSON.stringify(nums)}`);
|
||||
console.log(`すべての順列 res = ${JSON.stringify(res)}`);
|
||||
Reference in New Issue
Block a user