From 08b7474894e18822f3d10f8c867b40dd907d1179 Mon Sep 17 00:00:00 2001 From: sjinzh <99076655+sjinzh@users.noreply.github.com> Date: Thu, 9 Feb 2023 23:07:43 +0800 Subject: [PATCH] Update the coding style for Rust and Zig (#356) Co-authored-by: Yudong Jin --- .../leetcode_two_sum.rs | 48 ++++++++----------- .../leetcode_two_sum.zig | 19 ++++---- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs b/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs index 7fddae34e..8070f03e2 100644 --- a/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs +++ b/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs @@ -7,53 +7,47 @@ include!("../include/include.rs"); use std::collections::HashMap; -struct SolutionBruteForce; -struct SolutionHashMap; /* 方法一:暴力枚举 */ -impl SolutionBruteForce { - pub fn two_sum(nums: &Vec, target: i32) -> Vec { - 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]; - } +pub fn two_sum_brute_force(nums: &Vec, target: i32) -> Option> { + 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 Some(vec![i as i32, j as i32]); } } - vec![] } + None } /* 方法二:辅助哈希表 */ -impl SolutionHashMap { - pub fn two_sum(nums: &Vec, target: i32) -> Vec { - // 辅助哈希表,空间复杂度 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], - None => dic.insert(num, i as i32) - }; - } - vec![] +pub fn two_sum_hash_table(nums: &Vec, target: i32) -> Option> { + // 辅助哈希表,空间复杂度 O(n) + let mut dic = HashMap::new(); + // 单层循环,时间复杂度 O(n) + for (i, num) in nums.iter().enumerate() { + match dic.get(&(target - num)) { + Some(v) => return Some(vec![*v as i32, i as i32]), + None => dic.insert(num, i as i32) + }; } + 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); } diff --git a/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig b/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig index 533211625..5c696d927 100644 --- a/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig +++ b/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig @@ -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); }