This commit is contained in:
krahets
2024-03-21 04:22:07 +08:00
parent 35a07170c0
commit cfdb743939
52 changed files with 292 additions and 290 deletions

View File

@ -224,9 +224,8 @@ comments: true
/* 二分查找 */
func binarySearch(nums: [Int], target: Int) -> Int {
let n = nums.count
// 求解问题 f(0, n-1)
return dfs(nums: nums, target: target, i: 0, j: n - 1)
dfs(nums: nums, target: target, i: nums.startIndex, j: nums.endIndex - 1)
}
```

View File

@ -259,7 +259,7 @@ comments: true
func buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {
// 初始化哈希表,存储 inorder 元素到索引的映射
let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }
return dfs(preorder: preorder, inorderMap: inorderMap, i: 0, l: 0, r: inorder.count - 1)
return dfs(preorder: preorder, inorderMap: inorderMap, i: inorder.startIndex, l: inorder.startIndex, r: inorder.endIndex - 1)
}
```