mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-03 19:52:01 +08:00
Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
This commit is contained in:
@@ -9,32 +9,32 @@ package chapter_backtracking;
|
||||
import java.util.*;
|
||||
|
||||
public class subset_sum_i_naive {
|
||||
/* バックトラッキング:部分和 I */
|
||||
/* バックトラッキング:部分集合和 I */
|
||||
static void backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {
|
||||
// 部分集合の和が target に等しければ、解を記録
|
||||
// 部分集合の和が target と等しくなったら、解を記録する
|
||||
if (total == target) {
|
||||
res.add(new ArrayList<>(state));
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
// すべての選択肢を走査する
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
// 枝刈り:部分和が target を超える場合はその選択をスキップする
|
||||
// 枝刈り:部分集合の和が target を超える場合、この選択をスキップする
|
||||
if (total + choices[i] > target) {
|
||||
continue;
|
||||
}
|
||||
// 試行:選択を行い、要素と total を更新する
|
||||
// 試行:選択を行い、要素の和 total を更新する
|
||||
state.add(choices[i]);
|
||||
// 次の選択へ進む
|
||||
// 次の選択に進む
|
||||
backtrack(state, target, total + choices[i], choices, res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
// 復元:選択を取り消し、以前の状態に戻す
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 部分和 I を解く(重複部分集合を含む) */
|
||||
/* 部分集合和 I を解く(重複した部分集合を含む) */
|
||||
static List<List<Integer>> subsetSumINaive(int[] nums, int target) {
|
||||
List<Integer> state = new ArrayList<>(); // 状態(部分集合)
|
||||
int total = 0; // 部分和
|
||||
int total = 0; // 部分集合和
|
||||
List<List<Integer>> res = new ArrayList<>(); // 結果リスト(部分集合のリスト)
|
||||
backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
@@ -47,7 +47,7 @@ public class subset_sum_i_naive {
|
||||
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("注意: この方法の出力結果には重複した集合が含まれます");
|
||||
System.out.println("和が " + target + " になるすべての部分集合 res = " + res);
|
||||
System.out.println("この方法の出力結果には重複した集合が含まれることに注意");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user