diff --git a/codes/javascript/chapter_computational_complexity/time_complexity.js b/codes/javascript/chapter_computational_complexity/time_complexity.js index 1ce3ffbf0..9c877602f 100644 --- a/codes/javascript/chapter_computational_complexity/time_complexity.js +++ b/codes/javascript/chapter_computational_complexity/time_complexity.js @@ -3,19 +3,3 @@ * Created Time: 2023-01-02 * Author: RiverTwilight (contact@rene.wang) */ - -function algorithm_A(n) { - console.log(0); -} -// 算法 B 时间复杂度:线性阶 -function algorithm_B(n) { - for (var i = 0; i < n; i++) { - console.log(0); - } -} -// 算法 C 时间复杂度:常数阶 -function algorithm_C(n) { - for (var i = 0; i < 1000000; i++) { - console.log(0); - } -} diff --git a/codes/typescript/chapter_computational_complexity/time_complexity.ts b/codes/typescript/chapter_computational_complexity/time_complexity.ts index cc183265b..6f0c9737c 100644 --- a/codes/typescript/chapter_computational_complexity/time_complexity.ts +++ b/codes/typescript/chapter_computational_complexity/time_complexity.ts @@ -3,20 +3,3 @@ * Created Time: 2023-01-02 * Author: RiverTwilight (contact@rene.wang) */ - -// 算法 A 时间复杂度:常数阶 -function algorithm_A(n: number): void { - console.log(0); -} -// 算法 B 时间复杂度:线性阶 -function algorithm_B(n: number): void { - for (var i = 0; i < n; i++) { - console.log(0); - } -} -// 算法 C 时间复杂度:常数阶 -function algorithm_C(n: number): void { - for (var i = 0; i < 1000000; i++) { - console.log(0); - } -} diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index dd82b6a2a..8c2edb08a 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -233,7 +233,7 @@ $$ === "JavaScript" - ```js title="time_complexity.js" + ```js title="" // 算法 A 时间复杂度:常数阶 function algorithm_A(n) { console.log(0); @@ -255,7 +255,7 @@ $$ === "TypeScript" - ```typescript title="time_complexity.ts" + ```typescript title="" // 算法 A 时间复杂度:常数阶 function algorithm_A(n: number): void { console.log(0); @@ -406,12 +406,32 @@ $$ === "JavaScript" ```js title="" + function algorithm(n){ + var a = 1; // +1 + a += 1; // +1 + a *= 2; // +1 + // 循环 n 次 + for(var i = 0; i < n; i++){ // +1 + console.log(0) // +1 + } + + } ``` === "TypeScript" ```typescript title="" + function algorithm(n: number): void{ + var a: number = 1; // +1 + a += 1; // +1 + a *= 2; // +1 + // 循环 n 次 + for(var i = 0; i < n; i++){ // +1 + console.log(0) // +1 + } + + } ``` @@ -575,12 +595,40 @@ $$ === "JavaScript" ```js title="" + function algorithm(n) { + var a = 1; // +0(技巧 1) + a = a + n; // +0(技巧 1) + // +n(技巧 2) + for (var i = 0; i < 5 * n + 1; i++) { + console.log(0); + } + // +n*n(技巧 3) + for (var i = 0; i < 2 * n; i++) { + for (var j = 0; j < n + 1; j++) { + console.log(0); + } + } + } ``` === "TypeScript" ```typescript title="" + function algorithm(n: number): void { + var a: number = 1; // +0(技巧 1) + a = a + n; // +0(技巧 1) + // +n(技巧 2) + for (var i = 0; i < 5 * n + 1; i++) { + console.log(0); + } + // +n*n(技巧 3) + for (var i = 0; i < 2 * n; i++) { + for (var j = 0; j < n + 1; j++) { + console.log(0); + } + } + } ```