mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-08 13:21:19 +08:00
docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
77
ja/codes/java/chapter_backtracking/n_queens.java
Normal file
77
ja/codes/java/chapter_backtracking/n_queens.java
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* File: n_queens.java
|
||||
* Created Time: 2023-05-04
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class n_queens {
|
||||
/* バックトラッキングアルゴリズム:n クイーン */
|
||||
public static void backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,
|
||||
boolean[] cols, boolean[] diags1, boolean[] diags2) {
|
||||
// すべての行が配置されたら、解を記録
|
||||
if (row == n) {
|
||||
List<List<String>> copyState = new ArrayList<>();
|
||||
for (List<String> sRow : state) {
|
||||
copyState.add(new ArrayList<>(sRow));
|
||||
}
|
||||
res.add(copyState);
|
||||
return;
|
||||
}
|
||||
// すべての列を走査
|
||||
for (int col = 0; col < n; col++) {
|
||||
// セルに対応する主対角線と副対角線を計算
|
||||
int diag1 = row - col + n - 1;
|
||||
int diag2 = row + col;
|
||||
// 剪定:セルの列、主対角線、副対角線にクイーンを配置することを許可しない
|
||||
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
|
||||
// 試行:セルにクイーンを配置
|
||||
state.get(row).set(col, "Q");
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = true;
|
||||
// 次の行を配置
|
||||
backtrack(row + 1, n, state, res, cols, diags1, diags2);
|
||||
// 回退:セルを空のスポットに復元
|
||||
state.get(row).set(col, "#");
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* n クイーンを解く */
|
||||
public static List<List<List<String>>> nQueens(int n) {
|
||||
// n*n サイズのチェスボードを初期化、'Q' はクイーンを表し、'#' は空のスポットを表す
|
||||
List<List<String>> state = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<String> row = new ArrayList<>();
|
||||
for (int j = 0; j < n; j++) {
|
||||
row.add("#");
|
||||
}
|
||||
state.add(row);
|
||||
}
|
||||
boolean[] cols = new boolean[n]; // クイーンのある列を記録
|
||||
boolean[] diags1 = new boolean[2 * n - 1]; // クイーンのある主対角線を記録
|
||||
boolean[] diags2 = new boolean[2 * n - 1]; // クイーンのある副対角線を記録
|
||||
List<List<List<String>>> res = new ArrayList<>();
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int n = 4;
|
||||
List<List<List<String>>> res = nQueens(n);
|
||||
|
||||
System.out.println("チェスボードの次元を " + n + " として入力");
|
||||
System.out.println("クイーン配置解の総数 = " + res.size());
|
||||
for (List<List<String>> state : res) {
|
||||
System.out.println("--------------------");
|
||||
for (List<String> row : state) {
|
||||
System.out.println(row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
51
ja/codes/java/chapter_backtracking/permutations_i.java
Normal file
51
ja/codes/java/chapter_backtracking/permutations_i.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: permutations_i.java
|
||||
* Created Time: 2023-04-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class permutations_i {
|
||||
/* バックトラッキングアルゴリズム:順列 I */
|
||||
public static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
|
||||
// 状態の長さが要素数と等しくなったら、解を記録
|
||||
if (state.size() == choices.length) {
|
||||
res.add(new ArrayList<Integer>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪定:要素の重複選択を許可しない
|
||||
if (!selected[i]) {
|
||||
// 試行:選択を行い、状態を更新
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 次のラウンドの選択に進む
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:選択を取り消し、前の状態に復元
|
||||
selected[i] = false;
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 順列 I */
|
||||
static List<List<Integer>> permutationsI(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
||||
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 1, 2, 3 };
|
||||
|
||||
List<List<Integer>> res = permutationsI(nums);
|
||||
|
||||
System.out.println("入力配列 nums = " + Arrays.toString(nums));
|
||||
System.out.println("すべての順列 res = " + res);
|
||||
}
|
||||
}
|
||||
53
ja/codes/java/chapter_backtracking/permutations_ii.java
Normal file
53
ja/codes/java/chapter_backtracking/permutations_ii.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: permutations_ii.java
|
||||
* Created Time: 2023-04-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class permutations_ii {
|
||||
/* バックトラッキングアルゴリズム:順列 II */
|
||||
static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
|
||||
// 状態の長さが要素数と等しくなったら、解を記録
|
||||
if (state.size() == choices.length) {
|
||||
res.add(new ArrayList<Integer>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
Set<Integer> duplicated = new HashSet<Integer>();
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪定:要素の重複選択を許可せず、等しい要素の重複選択も許可しない
|
||||
if (!selected[i] && !duplicated.contains(choice)) {
|
||||
// 試行:選択を行い、状態を更新
|
||||
duplicated.add(choice); // 選択された要素値を記録
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 次のラウンドの選択に進む
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:選択を取り消し、前の状態に復元
|
||||
selected[i] = false;
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 順列 II */
|
||||
static List<List<Integer>> permutationsII(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
||||
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 1, 2, 2 };
|
||||
|
||||
List<List<Integer>> res = permutationsII(nums);
|
||||
|
||||
System.out.println("入力配列 nums = " + Arrays.toString(nums));
|
||||
System.out.println("すべての順列 res = " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: preorder_traversal_i_compact.java
|
||||
* Created Time: 2023-04-16
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class preorder_traversal_i_compact {
|
||||
static List<TreeNode> res;
|
||||
|
||||
/* 前順走査:例1 */
|
||||
static void preOrder(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.val == 7) {
|
||||
// 解を記録
|
||||
res.add(root);
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
|
||||
System.out.println("\n二分木を初期化");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// 前順走査
|
||||
res = new ArrayList<>();
|
||||
preOrder(root);
|
||||
|
||||
System.out.println("\n値7のノードをすべて出力");
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (TreeNode node : res) {
|
||||
vals.add(node.val);
|
||||
}
|
||||
System.out.println(vals);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: preorder_traversal_ii_compact.java
|
||||
* Created Time: 2023-04-16
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class preorder_traversal_ii_compact {
|
||||
static List<TreeNode> path;
|
||||
static List<List<TreeNode>> res;
|
||||
|
||||
/* 前順走査:例2 */
|
||||
static void preOrder(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
// 試行
|
||||
path.add(root);
|
||||
if (root.val == 7) {
|
||||
// 解を記録
|
||||
res.add(new ArrayList<>(path));
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
// 回退
|
||||
path.remove(path.size() - 1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
|
||||
System.out.println("\n二分木を初期化");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// 前順走査
|
||||
path = new ArrayList<>();
|
||||
res = new ArrayList<>();
|
||||
preOrder(root);
|
||||
|
||||
System.out.println("\nルートからノード7までのすべてのパスを出力");
|
||||
for (List<TreeNode> path : res) {
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (TreeNode node : path) {
|
||||
vals.add(node.val);
|
||||
}
|
||||
System.out.println(vals);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_compact.java
|
||||
* Created Time: 2023-04-16
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class preorder_traversal_iii_compact {
|
||||
static List<TreeNode> path;
|
||||
static List<List<TreeNode>> res;
|
||||
|
||||
/* 前順走査:例3 */
|
||||
static void preOrder(TreeNode root) {
|
||||
// 剪定
|
||||
if (root == null || root.val == 3) {
|
||||
return;
|
||||
}
|
||||
// 試行
|
||||
path.add(root);
|
||||
if (root.val == 7) {
|
||||
// 解を記録
|
||||
res.add(new ArrayList<>(path));
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
// 回退
|
||||
path.remove(path.size() - 1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
|
||||
System.out.println("\n二分木を初期化");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// 前順走査
|
||||
path = new ArrayList<>();
|
||||
res = new ArrayList<>();
|
||||
preOrder(root);
|
||||
|
||||
System.out.println("\nルートからノード7までのすべてのパスを出力、値3のノードは含まない");
|
||||
for (List<TreeNode> path : res) {
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (TreeNode node : path) {
|
||||
vals.add(node.val);
|
||||
}
|
||||
System.out.println(vals);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_template.java
|
||||
* Created Time: 2023-04-16
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class preorder_traversal_iii_template {
|
||||
/* 現在の状態が解かどうかを判定 */
|
||||
static boolean isSolution(List<TreeNode> state) {
|
||||
return !state.isEmpty() && state.get(state.size() - 1).val == 7;
|
||||
}
|
||||
|
||||
/* 解を記録 */
|
||||
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
res.add(new ArrayList<>(state));
|
||||
}
|
||||
|
||||
/* 現在の状態下で選択が合法かどうかを判定 */
|
||||
static boolean isValid(List<TreeNode> state, TreeNode choice) {
|
||||
return choice != null && choice.val != 3;
|
||||
}
|
||||
|
||||
/* 状態を更新 */
|
||||
static void makeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.add(choice);
|
||||
}
|
||||
|
||||
/* 状態を復元 */
|
||||
static void undoChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
|
||||
/* バックトラッキングアルゴリズム:例3 */
|
||||
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
||||
// 解かどうかをチェック
|
||||
if (isSolution(state)) {
|
||||
// 解を記録
|
||||
recordSolution(state, res);
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
for (TreeNode choice : choices) {
|
||||
// 剪定:選択が合法かどうかをチェック
|
||||
if (isValid(state, choice)) {
|
||||
// 試行:選択を行い、状態を更新
|
||||
makeChoice(state, choice);
|
||||
// 次のラウンドの選択に進む
|
||||
backtrack(state, Arrays.asList(choice.left, choice.right), res);
|
||||
// 回退:選択を取り消し、前の状態に復元
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
|
||||
System.out.println("\n二分木を初期化");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// バックトラッキングアルゴリズム
|
||||
List<List<TreeNode>> res = new ArrayList<>();
|
||||
backtrack(new ArrayList<>(), Arrays.asList(root), res);
|
||||
|
||||
System.out.println("\nルートからノード7までのすべてのパスを出力、パスには値3のノードを含まないことが要求される");
|
||||
for (List<TreeNode> path : res) {
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (TreeNode node : path) {
|
||||
vals.add(node.val);
|
||||
}
|
||||
System.out.println(vals);
|
||||
}
|
||||
}
|
||||
}
|
||||
55
ja/codes/java/chapter_backtracking/subset_sum_i.java
Normal file
55
ja/codes/java/chapter_backtracking/subset_sum_i.java
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: subset_sum_i.java
|
||||
* Created Time: 2023-06-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class subset_sum_i {
|
||||
/* バックトラッキングアルゴリズム:部分集合和 I */
|
||||
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
|
||||
// 部分集合の和がtargetと等しいとき、解を記録
|
||||
if (target == 0) {
|
||||
res.add(new ArrayList<>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
// 剪定二:startから走査を開始し、重複する部分集合の生成を回避
|
||||
for (int i = start; i < choices.length; i++) {
|
||||
// 剪定一:部分集合の和がtargetを超えた場合、即座にループを終了
|
||||
// 配列がソートされているため、後の要素はさらに大きく、部分集合の和は必ずtargetを超える
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 試行:選択を行い、target、startを更新
|
||||
state.add(choices[i]);
|
||||
// 次のラウンドの選択に進む
|
||||
backtrack(state, target - choices[i], choices, i, res);
|
||||
// 回退:選択を取り消し、前の状態に復元
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 部分集合和 I を解く */
|
||||
static List<List<Integer>> subsetSumI(int[] nums, int target) {
|
||||
List<Integer> state = new ArrayList<>(); // 状態(部分集合)
|
||||
Arrays.sort(nums); // nums をソート
|
||||
int start = 0; // 走査の開始点
|
||||
List<List<Integer>> res = new ArrayList<>(); // 結果リスト(部分集合リスト)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 3, 4, 5 };
|
||||
int target = 9;
|
||||
|
||||
List<List<Integer>> res = subsetSumI(nums, target);
|
||||
|
||||
System.out.println("入力配列 nums = " + Arrays.toString(nums) + ", target = " + target);
|
||||
System.out.println("和が " + target + " のすべての部分集合 res = " + res);
|
||||
}
|
||||
}
|
||||
53
ja/codes/java/chapter_backtracking/subset_sum_i_naive.java
Normal file
53
ja/codes/java/chapter_backtracking/subset_sum_i_naive.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: subset_sum_i_naive.java
|
||||
* Created Time: 2023-06-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class subset_sum_i_naive {
|
||||
/* バックトラッキングアルゴリズム:部分集合和 I */
|
||||
static void backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {
|
||||
// 部分集合の和がtargetと等しいとき、解を記録
|
||||
if (total == target) {
|
||||
res.add(new ArrayList<>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
// 剪定:部分集合の和がtargetを超えた場合、その選択をスキップ
|
||||
if (total + choices[i] > target) {
|
||||
continue;
|
||||
}
|
||||
// 試行:選択を行い、要素とtotalを更新
|
||||
state.add(choices[i]);
|
||||
// 次のラウンドの選択に進む
|
||||
backtrack(state, target, total + choices[i], choices, res);
|
||||
// 回退:選択を取り消し、前の状態に復元
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 部分集合和 I を解く(重複する部分集合を含む) */
|
||||
static List<List<Integer>> subsetSumINaive(int[] nums, int target) {
|
||||
List<Integer> state = new ArrayList<>(); // 状態(部分集合)
|
||||
int total = 0; // 部分集合の和
|
||||
List<List<Integer>> res = new ArrayList<>(); // 結果リスト(部分集合リスト)
|
||||
backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 3, 4, 5 };
|
||||
int target = 9;
|
||||
|
||||
List<List<Integer>> res = subsetSumINaive(nums, target);
|
||||
|
||||
System.out.println("入力配列 nums = " + Arrays.toString(nums) + ", target = " + target);
|
||||
System.out.println("和が " + target + " のすべての部分集合 res = " + res);
|
||||
System.out.println("この方法の結果には重複する集合が含まれています");
|
||||
}
|
||||
}
|
||||
60
ja/codes/java/chapter_backtracking/subset_sum_ii.java
Normal file
60
ja/codes/java/chapter_backtracking/subset_sum_ii.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: subset_sum_ii.java
|
||||
* Created Time: 2023-06-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class subset_sum_ii {
|
||||
/* バックトラッキングアルゴリズム:部分集合和 II */
|
||||
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
|
||||
// 部分集合の和がtargetと等しいとき、解を記録
|
||||
if (target == 0) {
|
||||
res.add(new ArrayList<>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
// 剪定二:startから走査を開始し、重複する部分集合の生成を回避
|
||||
// 剪定三:startから走査を開始し、同じ要素の繰り返し選択を回避
|
||||
for (int i = start; i < choices.length; i++) {
|
||||
// 剪定一:部分集合の和がtargetを超えた場合、即座にループを終了
|
||||
// 配列がソートされているため、後の要素はさらに大きく、部分集合の和は必ずtargetを超える
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 剪定四:要素が左の要素と等しい場合、検索ブランチの重複を示すのでスキップ
|
||||
if (i > start && choices[i] == choices[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
// 試行:選択を行い、target、startを更新
|
||||
state.add(choices[i]);
|
||||
// 次のラウンドの選択に進む
|
||||
backtrack(state, target - choices[i], choices, i + 1, res);
|
||||
// 回退:選択を取り消し、前の状態に復元
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 部分集合和 II を解く */
|
||||
static List<List<Integer>> subsetSumII(int[] nums, int target) {
|
||||
List<Integer> state = new ArrayList<>(); // 状態(部分集合)
|
||||
Arrays.sort(nums); // nums をソート
|
||||
int start = 0; // 走査の開始点
|
||||
List<List<Integer>> res = new ArrayList<>(); // 結果リスト(部分集合リスト)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 4, 5 };
|
||||
int target = 9;
|
||||
|
||||
List<List<Integer>> res = subsetSumII(nums, target);
|
||||
|
||||
System.out.println("入力配列 nums = " + Arrays.toString(nums) + ", target = " + target);
|
||||
System.out.println("和が " + target + " のすべての部分集合 res = " + res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user