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,62 @@
/**
* File: binary_search.swift
* Created Time: 2023-01-28
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func binarySearch(nums: [Int], target: Int) -> Int {
// [0, n-1] i, j
var i = nums.startIndex
var j = nums.endIndex - 1
// i > j
while i <= j {
let m = i + (j - i) / 2 // m
if nums[m] < target { // target [m+1, j]
i = m + 1
} else if nums[m] > target { // target [i, m-1]
j = m - 1
} else { //
return m
}
}
// -1
return -1
}
/* */
func binarySearchLCRO(nums: [Int], target: Int) -> Int {
// [0, n) i, j +1
var i = nums.startIndex
var j = nums.endIndex
// i = j
while i < j {
let m = i + (j - i) / 2 // m
if nums[m] < target { // target [m+1, j)
i = m + 1
} else if nums[m] > target { // target [i, m)
j = m
} else { //
return m
}
}
// -1
return -1
}
@main
enum BinarySearch {
/* Driver Code */
static func main() {
let target = 6
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
/* */
var index = binarySearch(nums: nums, target: target)
print("対象要素 6 のインデックス = \(index)")
/* */
index = binarySearchLCRO(nums: nums, target: target)
print("対象要素 6 のインデックス = \(index)")
}
}
@@ -0,0 +1,51 @@
/**
* File: binary_search_edge.swift
* Created Time: 2023-08-06
* Author: nuomi1 (nuomi1@qq.com)
*/
import binary_search_insertion_target
/* target */
func binarySearchLeftEdge(nums: [Int], target: Int) -> Int {
// target
let i = binarySearchInsertion(nums: nums, target: target)
// target -1
if i == nums.endIndex || nums[i] != target {
return -1
}
// target i
return i
}
/* target */
func binarySearchRightEdge(nums: [Int], target: Int) -> Int {
// target + 1
let i = binarySearchInsertion(nums: nums, target: target + 1)
// j target i target
let j = i - 1
// target -1
if j == -1 || nums[j] != target {
return -1
}
// target j
return j
}
@main
enum BinarySearchEdge {
/* Driver Code */
static func main() {
//
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
print("\n配列 nums = \(nums)")
//
for target in [6, 7] {
var index = binarySearchLeftEdge(nums: nums, target: target)
print("最も左にある要素 \(target) のインデックスは \(index)")
index = binarySearchRightEdge(nums: nums, target: target)
print("最も右にある要素 \(target) のインデックスは \(index)")
}
}
}
@@ -0,0 +1,71 @@
/**
* File: binary_search_insertion.swift
* Created Time: 2023-08-06
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {
// [0, n-1]
var i = nums.startIndex
var j = nums.endIndex - 1
while i <= j {
let m = i + (j - i) / 2 // m
if nums[m] < target {
i = m + 1 // target [m+1, j]
} else if nums[m] > target {
j = m - 1 // target [i, m-1]
} else {
return m // target m
}
}
// target i
return i
}
/* */
public func binarySearchInsertion(nums: [Int], target: Int) -> Int {
// [0, n-1]
var i = nums.startIndex
var j = nums.endIndex - 1
while i <= j {
let m = i + (j - i) / 2 // m
if nums[m] < target {
i = m + 1 // target [m+1, j]
} else if nums[m] > target {
j = m - 1 // target [i, m-1]
} else {
j = m - 1 // target [i, m-1]
}
}
// i
return i
}
#if !TARGET
@main
enum BinarySearchInsertion {
/* Driver Code */
static func main() {
//
var nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
print("\n配列 nums = \(nums)")
//
for target in [6, 9] {
let index = binarySearchInsertionSimple(nums: nums, target: target)
print("要素 \(target) の挿入位置のインデックスは \(index)")
}
//
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
print("\n配列 nums = \(nums)")
//
for target in [2, 6, 20] {
let index = binarySearchInsertion(nums: nums, target: target)
print("要素 \(target) の挿入位置のインデックスは \(index)")
}
}
}
#endif
@@ -0,0 +1,71 @@
/**
* File: binary_search_insertion.swift
* Created Time: 2023-08-06
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {
// [0, n-1]
var i = nums.startIndex
var j = nums.endIndex - 1
while i <= j {
let m = i + (j - i) / 2 // m
if nums[m] < target {
i = m + 1 // target [m+1, j]
} else if nums[m] > target {
j = m - 1 // target [i, m-1]
} else {
return m // target m
}
}
// target i
return i
}
/* */
public func binarySearchInsertion(nums: [Int], target: Int) -> Int {
// [0, n-1]
var i = nums.startIndex
var j = nums.endIndex - 1
while i <= j {
let m = i + (j - i) / 2 // m
if nums[m] < target {
i = m + 1 // target [m+1, j]
} else if nums[m] > target {
j = m - 1 // target [i, m-1]
} else {
j = m - 1 // target [i, m-1]
}
}
// i
return i
}
#if !TARGET
@main
enum BinarySearchInsertion {
/* Driver Code */
static func main() {
//
var nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
print("\n配列 nums = \(nums)")
//
for target in [6, 9] {
let index = binarySearchInsertionSimple(nums: nums, target: target)
print("要素 \(target) の挿入位置のインデックスは \(index)")
}
//
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
print("\n配列 nums = \(nums)")
//
for target in [2, 6, 20] {
let index = binarySearchInsertion(nums: nums, target: target)
print("要素 \(target) の挿入位置のインデックスは \(index)")
}
}
}
#endif
@@ -0,0 +1,50 @@
/**
* File: hashing_search.swift
* Created Time: 2023-01-28
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
func hashingSearchArray(map: [Int: Int], target: Int) -> Int {
// key: value:
// key -1
return map[target, default: -1]
}
/* */
func hashingSearchLinkedList(map: [Int: ListNode], target: Int) -> ListNode? {
// key: value:
// key null
return map[target]
}
@main
enum HashingSearch {
/* Driver Code */
static func main() {
let target = 3
/* */
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]
//
var map: [Int: Int] = [:]
for i in nums.indices {
map[nums[i]] = i // key: value:
}
let index = hashingSearchArray(map: map, target: target)
print("目標要素 3 のインデックス = \(index)")
/* */
var head = ListNode.arrToLinkedList(arr: nums)
//
var map1: [Int: ListNode] = [:]
while head != nil {
map1[head!.val] = head! // key: value:
head = head?.next
}
let node = hashingSearchLinkedList(map: map1, target: target)
print("目標ノード値 3 に対応するノードオブジェクトは \(node!)")
}
}
@@ -0,0 +1,53 @@
/**
* File: linear_search.swift
* Created Time: 2023-01-28
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
func linearSearchArray(nums: [Int], target: Int) -> Int {
//
for i in nums.indices {
//
if nums[i] == target {
return i
}
}
// -1
return -1
}
/* */
func linearSearchLinkedList(head: ListNode?, target: Int) -> ListNode? {
var head = head
//
while head != nil {
//
if head?.val == target {
return head
}
head = head?.next
}
// null
return nil
}
@main
enum LinearSearch {
/* Driver Code */
static func main() {
let target = 3
/* */
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]
let index = linearSearchArray(nums: nums, target: target)
print("目標要素 3 のインデックス = \(index)")
/* */
let head = ListNode.arrToLinkedList(arr: nums)
let node = linearSearchLinkedList(head: head, target: target)
print("目標ノード値 3 に対応するノードオブジェクトは \(node!)")
}
}
@@ -0,0 +1,49 @@
/**
* File: two_sum.swift
* Created Time: 2023-01-03
* Author: nuomi1 (nuomi1@qq.com)
*/
/* 1 */
func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
// 2 O(n^2)
for i in nums.indices.dropLast() {
for j in nums.indices.dropFirst(i + 1) {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
return [0]
}
/* 2 */
func twoSumHashTable(nums: [Int], target: Int) -> [Int] {
// 使 O(n)
var dic: [Int: Int] = [:]
// O(n)
for i in nums.indices {
if let j = dic[target - nums[i]] {
return [j, i]
}
dic[nums[i]] = i
}
return [0]
}
@main
enum LeetcodeTwoSum {
/* Driver Code */
static func main() {
// ======= Test Case =======
let nums = [2, 7, 11, 15]
let target = 13
// ====== Driver Code ======
// 1
var res = twoSumBruteForce(nums: nums, target: target)
print("方法1 res = \(res)")
// 2
res = twoSumHashTable(nums: nums, target: target)
print("方法2 res = \(res)")
}
}