mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-02 22:43:50 +08:00
docs: added Typescript and Javascript examples
Not sure whether these formats meet the requirement. If everything is okay I will continue to transcribe more:-)
This commit is contained in:
parent
5ea660a2da
commit
c94101a365
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user