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:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions
@@ -0,0 +1,67 @@
/**
* File: n_queens.swift
* Created Time: 2023-05-14
* Author: nuomi1 (nuomi1@qq.com)
*/
/* N */
func backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {
//
if row == n {
res.append(state)
return
}
//
for col in 0 ..< n {
//
let diag1 = row - col + n - 1
let diag2 = row + col
//
if !cols[col] && !diags1[diag1] && !diags2[diag2] {
//
state[row][col] = "Q"
cols[col] = true
diags1[diag1] = true
diags2[diag2] = true
//
backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
//
state[row][col] = "#"
cols[col] = false
diags1[diag1] = false
diags2[diag2] = false
}
}
}
/* N */
func nQueens(n: Int) -> [[[String]]] {
// n*n 'Q' '#'
var state = Array(repeating: Array(repeating: "#", count: n), count: n)
var cols = Array(repeating: false, count: n) //
var diags1 = Array(repeating: false, count: 2 * n - 1) //
var diags2 = Array(repeating: false, count: 2 * n - 1) //
var res: [[[String]]] = []
backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)
return res
}
@main
enum NQueens {
/* Driver Code */
static func main() {
let n = 4
let res = nQueens(n: n)
print("入力された盤面の縦横は \(n)")
print("クイーンの配置方法は全部で \(res.count) 通り")
for state in res {
print("--------------------")
for row in state {
print(row)
}
}
}
}
@@ -0,0 +1,50 @@
/**
* File: permutations_i.swift
* Created Time: 2023-04-30
* Author: nuomi1 (nuomi1@qq.com)
*/
/* I */
func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {
//
if state.count == choices.count {
res.append(state)
return
}
//
for (i, choice) in choices.enumerated() {
//
if !selected[i] {
// :
selected[i] = true
state.append(choice)
//
backtrack(state: &state, choices: choices, selected: &selected, res: &res)
//
selected[i] = false
state.removeLast()
}
}
}
/* I */
func permutationsI(nums: [Int]) -> [[Int]] {
var state: [Int] = []
var selected = Array(repeating: false, count: nums.count)
var res: [[Int]] = []
backtrack(state: &state, choices: nums, selected: &selected, res: &res)
return res
}
@main
enum PermutationsI {
/* Driver Code */
static func main() {
let nums = [1, 2, 3]
let res = permutationsI(nums: nums)
print("入力配列 nums = \(nums)")
print("すべての順列 res = \(res)")
}
}
@@ -0,0 +1,52 @@
/**
* File: permutations_ii.swift
* Created Time: 2023-04-30
* Author: nuomi1 (nuomi1@qq.com)
*/
/* II */
func backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {
//
if state.count == choices.count {
res.append(state)
return
}
//
var duplicated: Set<Int> = []
for (i, choice) in choices.enumerated() {
//
if !selected[i], !duplicated.contains(choice) {
// :
duplicated.insert(choice) //
selected[i] = true
state.append(choice)
//
backtrack(state: &state, choices: choices, selected: &selected, res: &res)
//
selected[i] = false
state.removeLast()
}
}
}
/* II */
func permutationsII(nums: [Int]) -> [[Int]] {
var state: [Int] = []
var selected = Array(repeating: false, count: nums.count)
var res: [[Int]] = []
backtrack(state: &state, choices: nums, selected: &selected, res: &res)
return res
}
@main
enum PermutationsII {
/* Driver Code */
static func main() {
let nums = [1, 2, 3]
let res = permutationsII(nums: nums)
print("入力配列 nums = \(nums)")
print("すべての順列 res = \(res)")
}
}
@@ -0,0 +1,43 @@
/**
* File: preorder_traversal_i_compact.swift
* Created Time: 2023-04-30
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
var res: [TreeNode] = []
/* 1 */
func preOrder(root: TreeNode?) {
guard let root = root else {
return
}
if root.val == 7 {
//
res.append(root)
}
preOrder(root: root.left)
preOrder(root: root.right)
}
@main
enum PreorderTraversalICompact {
/* Driver Code */
static func main() {
let root = TreeNode.listToTree(arr: [1, 7, 3, 4, 5, 6, 7])
print("\n二分木を初期化")
PrintUtil.printTree(root: root)
//
res = []
preOrder(root: root)
print("\n値が 7 のすべてのノードを出力")
var vals: [Int] = []
for node in res {
vals.append(node.val)
}
print(vals)
}
}
@@ -0,0 +1,51 @@
/**
* File: preorder_traversal_ii_compact.swift
* Created Time: 2023-04-30
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
var path: [TreeNode] = []
var res: [[TreeNode]] = []
/* 2 */
func preOrder(root: TreeNode?) {
guard let root = root else {
return
}
//
path.append(root)
if root.val == 7 {
//
res.append(path)
}
preOrder(root: root.left)
preOrder(root: root.right)
//
path.removeLast()
}
@main
enum PreorderTraversalIICompact {
/* Driver Code */
static func main() {
let root = TreeNode.listToTree(arr: [1, 7, 3, 4, 5, 6, 7])
print("\n二分木を初期化")
PrintUtil.printTree(root: root)
//
path = []
res = []
preOrder(root: root)
print("\n根ノードからノード 7 までのすべての経路を出力")
for path in res {
var vals: [Int] = []
for node in path {
vals.append(node.val)
}
print(vals)
}
}
}
@@ -0,0 +1,52 @@
/**
* File: preorder_traversal_iii_compact.swift
* Created Time: 2023-04-30
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
var path: [TreeNode] = []
var res: [[TreeNode]] = []
/* 3 */
func preOrder(root: TreeNode?) {
//
guard let root = root, root.val != 3 else {
return
}
//
path.append(root)
if root.val == 7 {
//
res.append(path)
}
preOrder(root: root.left)
preOrder(root: root.right)
//
path.removeLast()
}
@main
enum PreorderTraversalIIICompact {
/* Driver Code */
static func main() {
let root = TreeNode.listToTree(arr: [1, 7, 3, 4, 5, 6, 7])
print("\n二分木を初期化")
PrintUtil.printTree(root: root)
//
path = []
res = []
preOrder(root: root)
print("\n根ノードからノード 7 までのすべての経路を出力し、経路に値が 3 のノードを含まない")
for path in res {
var vals: [Int] = []
for node in path {
vals.append(node.val)
}
print(vals)
}
}
}
@@ -0,0 +1,76 @@
/**
* File: preorder_traversal_iii_template.swift
* Created Time: 2023-04-30
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
func isSolution(state: [TreeNode]) -> Bool {
!state.isEmpty && state.last!.val == 7
}
/* */
func recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {
res.append(state)
}
/* */
func isValid(state: [TreeNode], choice: TreeNode?) -> Bool {
choice != nil && choice!.val != 3
}
/* */
func makeChoice(state: inout [TreeNode], choice: TreeNode) {
state.append(choice)
}
/* */
func undoChoice(state: inout [TreeNode], choice: TreeNode) {
state.removeLast()
}
/* 3 */
func backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {
//
if isSolution(state: state) {
recordSolution(state: state, res: &res)
}
//
for choice in choices {
//
if isValid(state: state, choice: choice) {
// :
makeChoice(state: &state, choice: choice)
//
backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)
//
undoChoice(state: &state, choice: choice)
}
}
}
@main
enum PreorderTraversalIIITemplate {
/* Driver Code */
static func main() {
let root = TreeNode.listToTree(arr: [1, 7, 3, 4, 5, 6, 7])
print("\n二分木を初期化")
PrintUtil.printTree(root: root)
//
var state: [TreeNode] = []
var res: [[TreeNode]] = []
backtrack(state: &state, choices: [root].compactMap { $0 }, res: &res)
print("\n根ノードからノード 7 までのすべての経路を出力し、経路に値が 3 のノードを含まない")
for path in res {
var vals: [Int] = []
for node in path {
vals.append(node.val)
}
print(vals)
}
}
}
@@ -0,0 +1,53 @@
/**
* File: subset_sum_i.swift
* Created Time: 2023-07-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* I */
func backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {
// target
if target == 0 {
res.append(state)
return
}
//
// 2: start
for i in choices.indices.dropFirst(start) {
// 1 target
// target
if target - choices[i] < 0 {
break
}
// target start
state.append(choices[i])
//
backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)
//
state.removeLast()
}
}
/* I */
func subsetSumI(nums: [Int], target: Int) -> [[Int]] {
var state: [Int] = [] //
let nums = nums.sorted() // nums
let start = 0 //
var res: [[Int]] = [] //
backtrack(state: &state, target: target, choices: nums, start: start, res: &res)
return res
}
@main
enum SubsetSumI {
/* Driver Code */
static func main() {
let nums = [3, 4, 5]
let target = 9
let res = subsetSumI(nums: nums, target: target)
print("入力配列 nums = \(nums), target = \(target)")
print("和が \(target) に等しいすべての部分集合 res = \(res)")
}
}
@@ -0,0 +1,51 @@
/**
* File: subset_sum_i_naive.swift
* Created Time: 2023-07-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* I */
func backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {
// target
if total == target {
res.append(state)
return
}
//
for i in choices.indices {
// target
if total + choices[i] > target {
continue
}
// total
state.append(choices[i])
//
backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)
//
state.removeLast()
}
}
/* I */
func subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {
var state: [Int] = [] //
let total = 0 //
var res: [[Int]] = [] //
backtrack(state: &state, target: target, total: total, choices: nums, res: &res)
return res
}
@main
enum SubsetSumINaive {
/* Driver Code */
static func main() {
let nums = [3, 4, 5]
let target = 9
let res = subsetSumINaive(nums: nums, target: target)
print("入力配列 nums = \(nums), target = \(target)")
print("和が \(target) に等しいすべての部分集合 res = \(res)")
print("この方法の出力結果には重複した集合が含まれることに注意してください")
}
}
@@ -0,0 +1,58 @@
/**
* File: subset_sum_ii.swift
* Created Time: 2023-07-02
* Author: nuomi1 (nuomi1@qq.com)
*/
/* II */
func backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {
// target
if target == 0 {
res.append(state)
return
}
//
// 2: start
// 3: start
for i in choices.indices.dropFirst(start) {
// 1 target
// target
if target - choices[i] < 0 {
break
}
// 4
if i > start, choices[i] == choices[i - 1] {
continue
}
// target start
state.append(choices[i])
//
backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)
//
state.removeLast()
}
}
/* II */
func subsetSumII(nums: [Int], target: Int) -> [[Int]] {
var state: [Int] = [] //
let nums = nums.sorted() // nums
let start = 0 //
var res: [[Int]] = [] //
backtrack(state: &state, target: target, choices: nums, start: start, res: &res)
return res
}
@main
enum SubsetSumII {
/* Driver Code */
static func main() {
let nums = [4, 4, 5]
let target = 9
let res = subsetSumII(nums: nums, target: target)
print("入力配列 nums = \(nums), target = \(target)")
print("和が \(target) に等しいすべての部分集合 res = \(res)")
}
}