Update the coding style for Rust and Zig (#356)

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
sjinzh 2023-02-09 23:07:43 +08:00 committed by GitHub
parent ec25970e8e
commit 08b7474894
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 37 deletions

View file

@ -7,53 +7,47 @@
include!("../include/include.rs"); include!("../include/include.rs");
use std::collections::HashMap; use std::collections::HashMap;
struct SolutionBruteForce;
struct SolutionHashMap;
/* 方法一:暴力枚举 */ /* 方法一:暴力枚举 */
impl SolutionBruteForce { pub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> { let size = nums.len();
let size = nums.len(); // 两层循环,时间复杂度 O(n^2)
// 两层循环,时间复杂度 O(n^2) for i in 0..size - 1 {
for i in 0..size - 1 { for j in i + 1..size {
for j in i + 1..size { if nums[i] + nums[j] == target {
if nums[i] + nums[j] == target { return Some(vec![i as i32, j as i32]);
return vec![i as i32, j as i32];
}
} }
} }
vec![]
} }
None
} }
/* 方法二:辅助哈希表 */ /* 方法二:辅助哈希表 */
impl SolutionHashMap { pub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> { // 辅助哈希表,空间复杂度 O(n)
// 辅助哈希表,空间复杂度 O(n) let mut dic = HashMap::new();
let mut dic = HashMap::new(); // 单层循环,时间复杂度 O(n)
// 单层循环,时间复杂度 O(n) for (i, num) in nums.iter().enumerate() {
for (i, num) in nums.iter().enumerate() { match dic.get(&(target - num)) {
match dic.get(&(target - num)) { Some(v) => return Some(vec![*v as i32, i as i32]),
Some(v) => return vec![*v as i32, i as i32], None => dic.insert(num, i as i32)
None => dic.insert(num, i as i32) };
};
}
vec![]
} }
None
} }
/* Driver Code */
fn main() { fn main() {
// ======= Test Case ======= // ======= Test Case =======
let nums = vec![ 2, 7, 11, 15 ]; let nums = vec![ 2, 7, 11, 15 ];
let target = 9; let target = 9;
// ====== Driver Code ======
// 方法一 // 方法一
let res = SolutionBruteForce::two_sum(&nums, target); let res = two_sum_brute_force(&nums, target).unwrap();
print!("方法一 res = "); print!("方法一 res = ");
print_util::print_array(&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!("\n方法二 res = ");
print_util::print_array(&res); print_util::print_array(&res);
} }

View file

@ -6,8 +6,7 @@ const std = @import("std");
const inc = @import("include"); const inc = @import("include");
// //
pub fn twoSumBruteForce(nums: []i32, target: i32) [2]i32 { pub fn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {
_ = self;
var size: usize = nums.len; var size: usize = nums.len;
var i: usize = 0; var i: usize = 0;
// O(n^2) // 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 { pub fn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {
_ = self;
var size: usize = nums.len; var size: usize = nums.len;
// O(n) // O(n)
var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator); 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)); try dic.put(nums[i], @intCast(i32, i));
} }
return undefined; return null;
} }
// Driver Code
pub fn main() !void { pub fn main() !void {
// ======= Test Case ======= // ======= Test Case =======
var nums = [_]i32{ 2, 7, 11, 15 }; var nums = [_]i32{ 2, 7, 11, 15 };
var target: i32 = 9; var target: i32 = 9;
// ====== Driver Code ======
// //
twoSumBruteForce(&nums, target); var res = twoSumBruteForce(&nums, target).?;
std.debug.print("方法一 res = ", .{}); std.debug.print("方法一 res = ", .{});
inc.PrintUtil.printArray(i32, &res); inc.PrintUtil.printArray(i32, &res);
// //
twoSumHashTable(&nums, target); res = (try twoSumHashTable(&nums, target)).?;
std.debug.print("方法二 res = ", .{}); std.debug.print("\n方法二 res = ", .{});
inc.PrintUtil.printArray(i32, &res); inc.PrintUtil.printArray(i32, &res);
} }