From c94101a365a7b22f9457c9d18dccd29290adbc1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Wang?= <52880665+RiverTwilight@users.noreply.github.com> Date: Sun, 1 Jan 2023 19:29:45 +0800 Subject: [PATCH] docs: added Typescript and Javascript examples Not sure whether these formats meet the requirement. If everything is okay I will continue to transcribe more:-) --- .../time_complexity.md | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 539c9b5cb..75f2dd31e 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -79,13 +79,27 @@ $$ === "JavaScript" ```js title="" - + function algorithm(n) { + var a = 2; // 1 ns + a = a + 1; // 1 ns + a = a * 2; // 10 ns + for(var i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + console.log(0); // 5 ns + } + } ``` === "TypeScript" ```typescript title="" - + function algorithm(n: number): void { + var a: number = 2; // 1 ns + a = a + 1; // 1 ns + a = a * 2; // 10 ns + for(var i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + console.log(0); // 5 ns + } + } ``` === "C" @@ -220,13 +234,44 @@ $$ === "JavaScript" ```js title="" + // 算法 A 时间复杂度:常数阶 + 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); + } + } ``` === "TypeScript" ```typescript title="" - + // 算法 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); + } + } ``` === "C"