This commit is contained in:
krahets
2023-02-09 22:55:29 +08:00
parent 3dcf65c61b
commit 7d86e2dd11
18 changed files with 1029 additions and 71 deletions
@@ -177,23 +177,22 @@ comments: true
=== "Zig"
```zig title="leetcode_two_sum.zig"
const SolutionBruteForce = struct {
pub fn twoSum(self: *SolutionBruteForce, nums: []i32, target: i32) [2]i32 {
_ = self;
var size: usize = nums.len;
var i: usize = 0;
// 两层循环,时间复杂度 O(n^2)
while (i < size - 1) : (i += 1) {
var j = i + 1;
while (j < size) : (j += 1) {
if (nums[i] + nums[j] == target) {
return [_]i32{@intCast(i32, i), @intCast(i32, j)};
}
// 方法一:暴力枚举
fn twoSumBruteForce(nums: []i32, target: i32) [2]i32 {
_ = self;
var size: usize = nums.len;
var i: usize = 0;
// 两层循环,时间复杂度 O(n^2)
while (i < size - 1) : (i += 1) {
var j = i + 1;
while (j < size) : (j += 1) {
if (nums[i] + nums[j] == target) {
return [_]i32{@intCast(i32, i), @intCast(i32, j)};
}
}
return undefined;
}
};
return undefined;
}
```
### 方法二:辅助哈希表
@@ -361,22 +360,21 @@ comments: true
=== "Zig"
```zig title="leetcode_two_sum.zig"
const SolutionHashMap = struct {
pub fn twoSum(self: *SolutionHashMap, nums: []i32, target: i32) ![2]i32 {
_ = self;
var size: usize = nums.len;
// 辅助哈希表,空间复杂度 O(n)
var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);
defer dic.deinit();
var i: usize = 0;
// 单层循环,时间复杂度 O(n)
while (i < size) : (i += 1) {
if (dic.contains(target - nums[i])) {
return [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)};
}
try dic.put(nums[i], @intCast(i32, i));
// 方法二:辅助哈希表
fn twoSumHashTable(nums: []i32, target: i32) ![2]i32 {
_ = self;
var size: usize = nums.len;
// 辅助哈希表,空间复杂度 O(n)
var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);
defer dic.deinit();
var i: usize = 0;
// 单层循环,时间复杂度 O(n)
while (i < size) : (i += 1) {
if (dic.contains(target - nums[i])) {
return [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)};
}
return undefined;
try dic.put(nums[i], @intCast(i32, i));
}
};
return undefined;
}
```
@@ -1363,7 +1363,7 @@ $$
}
}
return count;
}
}
```
![time_complexity_constant_linear_quadratic](time_complexity.assets/time_complexity_constant_linear_quadratic.png)
@@ -1788,7 +1788,7 @@ $$
```zig title="time_complexity.zig"
// 指数阶(循环实现)
fn exponential(n: i32) i32{
fn exponential(n: i32) i32 {
var count: i32 = 0;
var bas: i32 = 1;
var i: i32 = 0;
@@ -1802,7 +1802,7 @@ $$
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
}
```
![time_complexity_exponential](time_complexity.assets/time_complexity_exponential.png)
@@ -1909,7 +1909,7 @@ $$
```zig title="time_complexity.zig"
// 指数阶(递归实现)
fn expRecur(n: i32) i32{
fn expRecur(n: i32) i32 {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
@@ -2054,8 +2054,7 @@ $$
```zig title="time_complexity.zig"
// 对数阶(循环实现)
fn logarithmic(n: f32) i32
{
fn logarithmic(n: f32) i32 {
var count: i32 = 0;
var n_var = n;
while (n_var > 1)
@@ -2171,8 +2170,7 @@ $$
```zig title="time_complexity.zig"
// 对数阶(递归实现)
fn logRecur(n: f32) i32
{
fn logRecur(n: f32) i32 {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
@@ -2324,8 +2322,7 @@ $$
```zig title="time_complexity.zig"
// 线性对数阶
fn linearLogRecur(n: f32) i32
{
fn linearLogRecur(n: f32) i32 {
if (n <= 1) return 1;
var count: i32 = linearLogRecur(n / 2) +
linearLogRecur(n / 2);