From 70dead5cd07e6b7e7055a88663f8553f7422f61d Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 3 Feb 2023 18:53:15 +0800 Subject: [PATCH] Update worst_best_time_complexity, leetcode_two_sum --- .../leetcode_two_sum.c | 4 +- .../worst_best_time_complexity.c | 2 + .../leetcode_two_sum.cpp | 2 + .../worst_best_time_complexity.cpp | 2 + .../leetcode_two_sum.cs | 2 + .../worst_best_time_complexity.cs | 2 + .../leetcode_two_sum.go | 4 +- .../worst_best_time_complexity.go | 2 + .../leetcode_two_sum.java | 2 + .../worst_best_time_complexity.java | 2 + .../leetcode_two_sum.js | 2 + .../worst_best_time_complexity.js | 2 + .../leetcode_two_sum.py | 2 + .../worst_best_time_complexity.py | 2 + codes/python/chapter_tree/avl_tree.py | 2 +- .../leetcode_two_sum.rs | 2 + .../worst_best_time_complexity.rs | 2 + .../leetcode_two_sum.swift | 2 + .../worst_best_time_complexity.swift | 2 + .../leetcode_two_sum.ts | 2 + .../worst_best_time_complexity.ts | 2 + .../leetcode_two_sum.zig | 2 + .../worst_best_time_complexity.zig | 2 + .../time_complexity.md | 134 +++--------------- 24 files changed, 63 insertions(+), 121 deletions(-) diff --git a/codes/c/chapter_computational_complexity/leetcode_two_sum.c b/codes/c/chapter_computational_complexity/leetcode_two_sum.c index bfc621aa0..7446fab97 100644 --- a/codes/c/chapter_computational_complexity/leetcode_two_sum.c +++ b/codes/c/chapter_computational_complexity/leetcode_two_sum.c @@ -6,7 +6,7 @@ #include "../include/include.h" -/* 暴力解法 */ +/* 方法一:暴力枚举 */ int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) { for (int i = 0; i < numsSize; ++i) { for (int j = i + 1; j < numsSize; ++j) { @@ -49,7 +49,7 @@ void insert(hashTable *h, int key, int val) { } } - +/* 方法二:辅助哈希表 */ int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) { hashTable *hashtable = NULL; for (int i = 0; i < numsSize; i++) { diff --git a/codes/c/chapter_computational_complexity/worst_best_time_complexity.c b/codes/c/chapter_computational_complexity/worst_best_time_complexity.c index e5cee821f..323b90fd5 100644 --- a/codes/c/chapter_computational_complexity/worst_best_time_complexity.c +++ b/codes/c/chapter_computational_complexity/worst_best_time_complexity.c @@ -27,6 +27,8 @@ int *randomNumbers(int n) { /* 查找数组 nums 中数字 1 所在索引 */ int findOne(int *nums, int n) { for (int i = 0; i < n; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) return i; } return -1; diff --git a/codes/cpp/chapter_computational_complexity/leetcode_two_sum.cpp b/codes/cpp/chapter_computational_complexity/leetcode_two_sum.cpp index f68f242b9..2f99bc007 100644 --- a/codes/cpp/chapter_computational_complexity/leetcode_two_sum.cpp +++ b/codes/cpp/chapter_computational_complexity/leetcode_two_sum.cpp @@ -6,6 +6,7 @@ #include "../include/include.hpp" +/* 方法一:暴力枚举 */ class SolutionBruteForce { public: vector twoSum(vector& nums, int target) { @@ -21,6 +22,7 @@ public: } }; +/* 方法二:辅助哈希表 */ class SolutionHashMap { public: vector twoSum(vector& nums, int target) { diff --git a/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp b/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp index c2916cb40..3ac92b353 100644 --- a/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp +++ b/codes/cpp/chapter_computational_complexity/worst_best_time_complexity.cpp @@ -23,6 +23,8 @@ vector randomNumbers(int n) { /* 查找数组 nums 中数字 1 所在索引 */ int findOne(vector& nums) { for (int i = 0; i < nums.size(); i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) return i; } diff --git a/codes/csharp/chapter_computational_complexity/leetcode_two_sum.cs b/codes/csharp/chapter_computational_complexity/leetcode_two_sum.cs index 6d45f99b3..c5182138c 100644 --- a/codes/csharp/chapter_computational_complexity/leetcode_two_sum.cs +++ b/codes/csharp/chapter_computational_complexity/leetcode_two_sum.cs @@ -8,6 +8,7 @@ using NUnit.Framework; namespace hello_algo.chapter_computational_complexity { + /* 方法一:暴力枚举 */ class SolutionBruteForce { public int[] twoSum(int[] nums, int target) @@ -26,6 +27,7 @@ namespace hello_algo.chapter_computational_complexity } } + /* 方法二:辅助哈希表 */ class SolutionHashMap { public int[] twoSum(int[] nums, int target) diff --git a/codes/csharp/chapter_computational_complexity/worst_best_time_complexity.cs b/codes/csharp/chapter_computational_complexity/worst_best_time_complexity.cs index 9759cd584..4469fd976 100644 --- a/codes/csharp/chapter_computational_complexity/worst_best_time_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/worst_best_time_complexity.cs @@ -37,6 +37,8 @@ namespace hello_algo.chapter_computational_complexity { for (int i = 0; i < nums.Length; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) return i; } diff --git a/codes/go/chapter_computational_complexity/leetcode_two_sum.go b/codes/go/chapter_computational_complexity/leetcode_two_sum.go index 6712d97ec..663020381 100644 --- a/codes/go/chapter_computational_complexity/leetcode_two_sum.go +++ b/codes/go/chapter_computational_complexity/leetcode_two_sum.go @@ -4,7 +4,7 @@ package chapter_computational_complexity -// twoSumBruteForce +/* 方法一:暴力枚举 */ func twoSumBruteForce(nums []int, target int) []int { size := len(nums) // 两层循环,时间复杂度 O(n^2) @@ -18,7 +18,7 @@ func twoSumBruteForce(nums []int, target int) []int { return nil } -// twoSumHashTable +/* 方法二:辅助哈希表 */ func twoSumHashTable(nums []int, target int) []int { // 辅助哈希表,空间复杂度 O(n) hashTable := map[int]int{} diff --git a/codes/go/chapter_computational_complexity/worst_best_time_complexity.go b/codes/go/chapter_computational_complexity/worst_best_time_complexity.go index 10e475d59..64ad4dc9c 100644 --- a/codes/go/chapter_computational_complexity/worst_best_time_complexity.go +++ b/codes/go/chapter_computational_complexity/worst_best_time_complexity.go @@ -26,6 +26,8 @@ func randomNumbers(n int) []int { /* 查找数组 nums 中数字 1 所在索引 */ func findOne(nums []int) int { for i := 0; i < len(nums); i++ { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if nums[i] == 1 { return i } diff --git a/codes/java/chapter_computational_complexity/leetcode_two_sum.java b/codes/java/chapter_computational_complexity/leetcode_two_sum.java index b9f8bf4d7..3e3fe8a8f 100644 --- a/codes/java/chapter_computational_complexity/leetcode_two_sum.java +++ b/codes/java/chapter_computational_complexity/leetcode_two_sum.java @@ -8,6 +8,7 @@ package chapter_computational_complexity; import java.util.*; +/* 方法一:暴力枚举 */ class SolutionBruteForce { public int[] twoSum(int[] nums, int target) { int size = nums.length; @@ -22,6 +23,7 @@ class SolutionBruteForce { } } +/* 方法二:辅助哈希表 */ class SolutionHashMap { public int[] twoSum(int[] nums, int target) { int size = nums.length; diff --git a/codes/java/chapter_computational_complexity/worst_best_time_complexity.java b/codes/java/chapter_computational_complexity/worst_best_time_complexity.java index c0fe0baa7..5b6649046 100644 --- a/codes/java/chapter_computational_complexity/worst_best_time_complexity.java +++ b/codes/java/chapter_computational_complexity/worst_best_time_complexity.java @@ -29,6 +29,8 @@ public class worst_best_time_complexity { /* 查找数组 nums 中数字 1 所在索引 */ static int findOne(int[] nums) { for (int i = 0; i < nums.length; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) return i; } diff --git a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js index 0db9cd705..44989883f 100644 --- a/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js +++ b/codes/javascript/chapter_computational_complexity/leetcode_two_sum.js @@ -4,6 +4,7 @@ * Author: gyt95 (gytkwan@gmail.com) */ +/* 方法一:暴力枚举 */ function twoSumBruteForce(nums, target) { const n = nums.length; // 两层循环,时间复杂度 O(n^2) @@ -17,6 +18,7 @@ function twoSumBruteForce(nums, target) { return []; } +/* 方法二:辅助哈希表 */ function twoSumHashTable(nums, target) { // 辅助哈希表,空间复杂度 O(n) let m = {}; diff --git a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js index 949e50b04..a0a4df3bb 100644 --- a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js @@ -24,6 +24,8 @@ function randomNumbers(n) { /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums) { for (let i = 0; i < nums.length; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] === 1) { return i; } diff --git a/codes/python/chapter_computational_complexity/leetcode_two_sum.py b/codes/python/chapter_computational_complexity/leetcode_two_sum.py index cb3bf3b02..e1c0c848c 100644 --- a/codes/python/chapter_computational_complexity/leetcode_two_sum.py +++ b/codes/python/chapter_computational_complexity/leetcode_two_sum.py @@ -8,6 +8,7 @@ import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * +""" 方法一:暴力枚举 """ class SolutionBruteForce: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums) - 1): @@ -16,6 +17,7 @@ class SolutionBruteForce: return i, j return [] +""" 方法二:辅助哈希表 """ class SolutionHashMap: def twoSum(self, nums: List[int], target: int) -> List[int]: dic = {} diff --git a/codes/python/chapter_computational_complexity/worst_best_time_complexity.py b/codes/python/chapter_computational_complexity/worst_best_time_complexity.py index b70dce4f0..ed1a9101f 100644 --- a/codes/python/chapter_computational_complexity/worst_best_time_complexity.py +++ b/codes/python/chapter_computational_complexity/worst_best_time_complexity.py @@ -19,6 +19,8 @@ def random_numbers(n): """ 查找数组 nums 中数字 1 所在索引 """ def find_one(nums): for i in range(len(nums)): + # 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + # 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if nums[i] == 1: return i return -1 diff --git a/codes/python/chapter_tree/avl_tree.py b/codes/python/chapter_tree/avl_tree.py index 79f2e81d5..bedd5c37f 100644 --- a/codes/python/chapter_tree/avl_tree.py +++ b/codes/python/chapter_tree/avl_tree.py @@ -8,7 +8,7 @@ import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * - +""" AVL 树 """ class AVLTree: def __init__(self, root: Optional[TreeNode] = None): self.root = root diff --git a/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs b/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs index 821d52c92..f44dc7146 100644 --- a/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs +++ b/codes/rust/chapter_computational_complexity/leetcode_two_sum.rs @@ -8,6 +8,7 @@ use std::collections::HashMap; struct SolutionBruteForce; struct SolutionHashMap; +/* 方法一:暴力枚举 */ impl SolutionBruteForce { pub fn two_sum(nums: &Vec, target: i32) -> Vec { for i in 0..nums.len() - 1 { @@ -21,6 +22,7 @@ impl SolutionBruteForce { } } +/* 方法二:辅助哈希表 */ impl SolutionHashMap { pub fn two_sum(nums: &Vec, target: i32) -> Vec { let mut hm = HashMap::new(); diff --git a/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs b/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs index a5a979c58..c167d0236 100644 --- a/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs +++ b/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs @@ -19,6 +19,8 @@ fn random_numbers(n: i32) -> Vec { /* 查找数组 nums 中数字 1 所在索引 */ fn find_one(nums: &[i32]) -> Option { for i in 0..nums.len() { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if nums[i] == 1 { return Some(i); } diff --git a/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift b/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift index e82872d38..3ecf97b05 100644 --- a/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift +++ b/codes/swift/chapter_computational_complexity/leetcode_two_sum.swift @@ -4,6 +4,7 @@ * Author: nuomi1 (nuomi1@qq.com) */ +/* 方法一:暴力枚举 */ func twoSumBruteForce(nums: [Int], target: Int) -> [Int] { // 两层循环,时间复杂度 O(n^2) for i in nums.indices.dropLast() { @@ -16,6 +17,7 @@ func twoSumBruteForce(nums: [Int], target: Int) -> [Int] { return [0] } +/* 方法二:辅助哈希表 */ func twoSumHashTable(nums: [Int], target: Int) -> [Int] { // 辅助哈希表,空间复杂度 O(n) var dic: [Int: Int] = [:] diff --git a/codes/swift/chapter_computational_complexity/worst_best_time_complexity.swift b/codes/swift/chapter_computational_complexity/worst_best_time_complexity.swift index 3dac661d2..1844f1bde 100644 --- a/codes/swift/chapter_computational_complexity/worst_best_time_complexity.swift +++ b/codes/swift/chapter_computational_complexity/worst_best_time_complexity.swift @@ -16,6 +16,8 @@ func randomNumbers(n: Int) -> [Int] { /* 查找数组 nums 中数字 1 所在索引 */ func findOne(nums: [Int]) -> Int { for i in nums.indices { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if nums[i] == 1 { return i } diff --git a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts index 1759c089b..e78c4176c 100644 --- a/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts +++ b/codes/typescript/chapter_computational_complexity/leetcode_two_sum.ts @@ -4,6 +4,7 @@ * Author: gyt95 (gytkwan@gmail.com) */ +/* 方法一:暴力枚举 */ function twoSumBruteForce(nums: number[], target: number): number[] { const n = nums.length; // 两层循环,时间复杂度 O(n^2) @@ -17,6 +18,7 @@ function twoSumBruteForce(nums: number[], target: number): number[] { return []; }; +/* 方法二:辅助哈希表 */ function twoSumHashTable(nums: number[], target: number): number[] { // 辅助哈希表,空间复杂度 O(n) let m: Map = new Map(); diff --git a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts index fecf7c6fe..d273ee2bc 100644 --- a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts @@ -24,6 +24,8 @@ function randomNumbers(n: number): number[] { /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums: number[]): number { for (let i = 0; i < nums.length; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] === 1) { return i; } diff --git a/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig b/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig index 66b96f95e..f68e7f957 100644 --- a/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig +++ b/codes/zig/chapter_computational_complexity/leetcode_two_sum.zig @@ -5,6 +5,7 @@ const std = @import("std"); const inc = @import("include"); +// 方法一:暴力枚举 const SolutionBruteForce = struct { pub fn twoSum(self: *SolutionBruteForce, nums: []i32, target: i32) [2]i32 { _ = self; @@ -23,6 +24,7 @@ const SolutionBruteForce = struct { } }; +// 方法二:辅助哈希表 const SolutionHashMap = struct { pub fn twoSum(self: *SolutionHashMap, nums: []i32, target: i32) ![2]i32 { _ = self; diff --git a/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig b/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig index 0cdd61ebc..787635731 100644 --- a/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig +++ b/codes/zig/chapter_computational_complexity/worst_best_time_complexity.zig @@ -21,6 +21,8 @@ pub fn randomNumbers(comptime n: usize) [n]i32 { // 查找数组 nums 中数字 1 所在索引 pub fn findOne(nums: []i32) i32 { for (nums) |num, i| { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (num == 1) return @intCast(i32, i); } return -1; diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index d06fd27f5..c7eaa5336 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -2432,22 +2432,13 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ int findOne(int[] nums) { for (int i = 0; i < nums.length; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) return i; } return -1; } - - /* Driver Code */ - public void main(String[] args) { - for (int i = 0; i < 10; i++) { - int n = 100; - int[] nums = randomNumbers(n); - int index = findOne(nums); - System.out.println("打乱后的数组为 " + Arrays.toString(nums)); - System.out.println("数字 1 的索引为 " + index); - } - } } ``` @@ -2471,25 +2462,13 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ int findOne(vector& nums) { for (int i = 0; i < nums.size(); i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) return i; } return -1; } - - - /* Driver Code */ - int main() { - for (int i = 0; i < 1000; i++) { - int n = 100; - vector nums = randomNumbers(n); - int index = findOne(nums); - cout << "\n数组 [ 1, 2, ..., n ] 被打乱后 = "; - PrintUtil::printVector(nums); - cout << "数字 1 的索引为 " << index << endl; - } - return 0; - } ``` === "Python" @@ -2506,18 +2485,11 @@ $$ """ 查找数组 nums 中数字 1 所在索引 """ def find_one(nums): for i in range(len(nums)): + # 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + # 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if nums[i] == 1: return i return -1 - - """ Driver Code """ - if __name__ == "__main__": - for i in range(10): - n = 100 - nums = random_numbers(n) - index = find_one(nums) - print("\n数组 [ 1, 2, ..., n ] 被打乱后 =", nums) - print("数字 1 的索引为", index) ``` === "Go" @@ -2540,23 +2512,14 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ func findOne(nums []int) int { for i := 0; i < len(nums); i++ { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if nums[i] == 1 { return i } } return -1 } - - /* Driver Code */ - func main() { - for i := 0; i < 10; i++ { - n := 100 - nums := randomNumbers(n) - index := findOne(nums) - fmt.Println("\n数组 [ 1, 2, ..., n ] 被打乱后 =", nums) - fmt.Println("数字 1 的索引为", index) - } - } ``` === "JavaScript" @@ -2582,25 +2545,14 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums) { for (let i = 0; i < nums.length; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] === 1) { return i; } } return -1; } - - /* Driver Code */ - function main() { - for (let i = 0; i < 10; i++) { - let n = 100; - let nums = randomNumbers(n); - let index = findOne(nums); - console.log( - "\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]" - ); - console.log("数字 1 的索引为 " + index); - } - } ``` === "TypeScript" @@ -2626,25 +2578,14 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ function findOne(nums: number[]): number { for (let i = 0; i < nums.length; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] === 1) { return i; } } return -1; } - - /* Driver Code */ - function main(): void { - for (let i = 0; i < 10; i++) { - let n = 100; - let nums = randomNumbers(n); - let index = findOne(nums); - console.log( - "\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]" - ); - console.log("数字 1 的索引为 " + index); - } - } ``` === "C" @@ -2671,31 +2612,12 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ int findOne(int *nums, int n) { for (int i = 0; i < n; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) return i; } return -1; } - - /* Driver Code */ - int main(int argc, char *argv[]) { - // 初始化随机数种子 - srand((unsigned int)time(NULL)); - for (int i = 0; i < 10; i++) { - int n = 100; - int *nums = randomNumbers(n); - int index = findOne(nums, n); - printf("\n数组 [ 1, 2, ..., n ] 被打乱后 = "); - printArray(nums, n); - printf("数字 1 的索引为 %d\n", index); - // 释放堆区内存 - if (nums != NULL) { - free(nums); - nums = NULL; - } - } - getchar(); - return 0; - } ``` === "C#" @@ -2728,24 +2650,13 @@ $$ { for (int i = 0; i < nums.Length; i++) { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if (nums[i] == 1) return i; } return -1; } - - /* Driver Code */ - public void main(String[] args) - { - for (int i = 0; i < 10; i++) - { - int n = 100; - int[] nums = randomNumbers(n); - int index = findOne(nums); - Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums)); - Console.WriteLine("数字 1 的索引为 " + index); - } - } ``` === "Swift" @@ -2763,23 +2674,14 @@ $$ /* 查找数组 nums 中数字 1 所在索引 */ func findOne(nums: [Int]) -> Int { for i in nums.indices { + // 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + // 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) if nums[i] == 1 { return i } } return -1 } - - /* Driver Code */ - func main() { - for _ in 0 ..< 10 { - let n = 100 - let nums = randomNumbers(n: n) - let index = findOne(nums: nums) - print("数组 [ 1, 2, ..., n ] 被打乱后 = \(nums)") - print("数字 1 的索引为 \(index)") - } - } ``` === "Zig"