mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 09:49:49 +08:00
Update worst_best_time_complexity,
leetcode_two_sum
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
|
||||
// 方法一:暴力枚举
|
||||
const SolutionBruteForce = struct {
|
||||
pub fn twoSum(self: *SolutionBruteForce, nums: []i32, target: i32) [2]i32 {
|
||||
_ = self;
|
||||
@@ -23,6 +24,7 @@ const SolutionBruteForce = struct {
|
||||
}
|
||||
};
|
||||
|
||||
// 方法二:辅助哈希表
|
||||
const SolutionHashMap = struct {
|
||||
pub fn twoSum(self: *SolutionHashMap, nums: []i32, target: i32) ![2]i32 {
|
||||
_ = self;
|
||||
|
||||
@@ -21,6 +21,8 @@ pub fn randomNumbers(comptime n: usize) [n]i32 {
|
||||
// 查找数组 nums 中数字 1 所在索引
|
||||
pub fn findOne(nums: []i32) i32 {
|
||||
for (nums) |num, i| {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (num == 1) return @intCast(i32, i);
|
||||
}
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user