mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Update worst_best_time_complexity,
leetcode_two_sum
This commit is contained in:
@ -8,6 +8,7 @@ use std::collections::HashMap;
|
||||
struct SolutionBruteForce;
|
||||
struct SolutionHashMap;
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
impl SolutionBruteForce {
|
||||
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> {
|
||||
for i in 0..nums.len() - 1 {
|
||||
@ -21,6 +22,7 @@ impl SolutionBruteForce {
|
||||
}
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
impl SolutionHashMap {
|
||||
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> {
|
||||
let mut hm = HashMap::new();
|
||||
|
||||
@ -19,6 +19,8 @@ fn random_numbers(n: i32) -> Vec<i32> {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
fn find_one(nums: &[i32]) -> Option<usize> {
|
||||
for i in 0..nums.len() {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return Some(i);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user