mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 13:16:29 +08:00
Update the coding style for Rust and Zig (#356)
Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
parent
ec25970e8e
commit
08b7474894
2 changed files with 30 additions and 37 deletions
|
@ -7,53 +7,47 @@
|
|||
include!("../include/include.rs");
|
||||
|
||||
use std::collections::HashMap;
|
||||
struct SolutionBruteForce;
|
||||
struct SolutionHashMap;
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
impl SolutionBruteForce {
|
||||
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> {
|
||||
pub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
|
||||
let size = nums.len();
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for i in 0..size - 1 {
|
||||
for j in i + 1..size {
|
||||
if nums[i] + nums[j] == target {
|
||||
return vec![i as i32, j as i32];
|
||||
return Some(vec![i as i32, j as i32]);
|
||||
}
|
||||
}
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
impl SolutionHashMap {
|
||||
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> {
|
||||
pub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
let mut dic = HashMap::new();
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
match dic.get(&(target - num)) {
|
||||
Some(v) => return vec![*v as i32, i as i32],
|
||||
Some(v) => return Some(vec![*v as i32, i as i32]),
|
||||
None => dic.insert(num, i as i32)
|
||||
};
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// ======= Test Case =======
|
||||
let nums = vec![ 2, 7, 11, 15 ];
|
||||
let target = 9;
|
||||
|
||||
// ====== Driver Code ======
|
||||
// 方法一
|
||||
let res = SolutionBruteForce::two_sum(&nums, target);
|
||||
let res = two_sum_brute_force(&nums, target).unwrap();
|
||||
print!("方法一 res = ");
|
||||
print_util::print_array(&res);
|
||||
// 方法二
|
||||
let res = SolutionHashMap::two_sum(&nums, target);
|
||||
let res = two_sum_hash_table(&nums, target).unwrap();
|
||||
print!("\n方法二 res = ");
|
||||
print_util::print_array(&res);
|
||||
}
|
||||
|
|
|
@ -6,8 +6,7 @@ const std = @import("std");
|
|||
const inc = @import("include");
|
||||
|
||||
// 方法一:暴力枚举
|
||||
pub fn twoSumBruteForce(nums: []i32, target: i32) [2]i32 {
|
||||
_ = self;
|
||||
pub fn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {
|
||||
var size: usize = nums.len;
|
||||
var i: usize = 0;
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
|
@ -19,12 +18,11 @@ pub fn twoSumBruteForce(nums: []i32, target: i32) [2]i32 {
|
|||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
return null;
|
||||
}
|
||||
|
||||
// 方法二:辅助哈希表
|
||||
pub fn twoSumHashTable(nums: []i32, target: i32) ![2]i32 {
|
||||
_ = self;
|
||||
pub fn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {
|
||||
var size: usize = nums.len;
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);
|
||||
|
@ -37,21 +35,22 @@ pub fn twoSumHashTable(nums: []i32, target: i32) ![2]i32 {
|
|||
}
|
||||
try dic.put(nums[i], @intCast(i32, i));
|
||||
}
|
||||
return undefined;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
// ======= Test Case =======
|
||||
var nums = [_]i32{ 2, 7, 11, 15 };
|
||||
var target: i32 = 9;
|
||||
|
||||
// ====== Driver Code ======
|
||||
// 方法一
|
||||
twoSumBruteForce(&nums, target);
|
||||
var res = twoSumBruteForce(&nums, target).?;
|
||||
std.debug.print("方法一 res = ", .{});
|
||||
inc.PrintUtil.printArray(i32, &res);
|
||||
// 方法二
|
||||
twoSumHashTable(&nums, target);
|
||||
std.debug.print("方法二 res = ", .{});
|
||||
res = (try twoSumHashTable(&nums, target)).?;
|
||||
std.debug.print("\n方法二 res = ", .{});
|
||||
inc.PrintUtil.printArray(i32, &res);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue