Files
Ikko Eltociear Ashimine 954c45864b 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>
2025-10-17 05:04:43 +08:00

55 lines
2.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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);
}
}