This commit is contained in:
krahets
2023-07-11 01:02:48 +08:00
parent b1544c92ca
commit cd145751e2
9 changed files with 449 additions and 48 deletions

View File

@@ -271,7 +271,7 @@ comments: true
} else if (nums.items[m] > target) { // 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
} else { // 找到目标元素,返回其索引
return @intCast(T, m);
return @intCast(m);
}
}
// 未找到目标元素,返回 -1
@@ -538,7 +538,7 @@ comments: true
} else if (nums.items[m] > target) { // 此情况说明 target 在区间 [i, m) 中
j = m;
} else { // 找到目标元素,返回其索引
return @intCast(T, m);
return @intCast(m);
}
}
// 未找到目标元素,返回 -1

View File

@@ -185,7 +185,7 @@ comments: true
var j = i + 1;
while (j < size) : (j += 1) {
if (nums[i] + nums[j] == target) {
return [_]i32{@intCast(i32, i), @intCast(i32, j)};
return [_]i32{@intCast(i), @intCast(j)};
}
}
}
@@ -437,9 +437,9 @@ comments: true
// 单层循环,时间复杂度 O(n)
while (i < size) : (i += 1) {
if (dic.contains(target - nums[i])) {
return [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)};
return [_]i32{dic.get(target - nums[i]).?, @intCast(i)};
}
try dic.put(nums[i], @intCast(i32, i));
try dic.put(nums[i], @intCast(i));
}
return null;
}