From 8031e0e2c541846d90240b72431fca00b04b5c0e Mon Sep 17 00:00:00 2001 From: RiverTwilight Date: Thu, 5 Jan 2023 10:20:19 +0800 Subject: [PATCH] lint: added missing keyword --- .../worst_best_time_complexity.js | 2 +- .../worst_best_time_complexity.ts | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts 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 db966b6fd..f887fe28d 100644 --- a/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/worst_best_time_complexity.js @@ -5,7 +5,7 @@ */ function randomNumbers(n) { - nums = Array(n); + let nums = Array(n); for (let i = 0; i < n; i++) { nums[i] = i + 1; } diff --git a/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts new file mode 100644 index 000000000..b263f994d --- /dev/null +++ b/codes/typescript/chapter_computational_complexity/worst_best_time_complexity.ts @@ -0,0 +1,41 @@ +/* + * File: worst_best_time_complexity.ts + * Created Time: 2023-01-05 + * Author: RiverTwilight (contact@rene.wang) + */ + +function randomNumbers(n: number): number[] { + nums = Array(n); + for (let i = 0; i < n; i++) { + nums[i] = i + 1; + } + // 随机打乱数组元素 + for (let i = 0; i < n; i++) { + let r = Math.floor(Math.random() * n); + let temp = nums[i]; + nums[i] = nums[r]; + nums[r] = temp; + } + return nums; +} + +function findOne(nums) { + for (let i = 0; i < nums.length; i++) { + if (nums[i] == 1) { + return i; + } + } + return -1; +} + +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); + } +}