Skip to content

Commit 3c72728

Browse files
authored
feat: add typescript solution to lc problem: No.0050.Pow(x, n) (doocs#469)
1 parent 2f52037 commit 3c72728

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

solution/0000-0099/0050.Pow(x, n)/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ class Solution {
8585
}
8686
```
8787

88+
### **TypeScript**
89+
90+
```ts
91+
function myPow(x: number, n: number): number {
92+
let res = 1;
93+
if (n < 0) {
94+
n = -n;
95+
x = 1 / x;
96+
}
97+
for (let i = n; i != 0; i = Math.floor(i / 2)) {
98+
if ((i & 1) == 1) {
99+
res *= x;
100+
}
101+
x *= x;
102+
}
103+
return res;
104+
};
105+
```
106+
88107
### **...**
89108

90109
```

solution/0000-0099/0050.Pow(x, n)/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ class Solution {
7575
}
7676
```
7777

78+
### **TypeScript**
79+
80+
```ts
81+
function myPow(x: number, n: number): number {
82+
let res = 1;
83+
if (n < 0) {
84+
n = -n;
85+
x = 1 / x;
86+
}
87+
for (let i = n; i != 0; i = Math.floor(i / 2)) {
88+
if ((i & 1) == 1) {
89+
res *= x;
90+
}
91+
x *= x;
92+
}
93+
return res;
94+
};
95+
```
96+
7897
### **...**
7998

8099
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function myPow(x: number, n: number): number {
2+
let res = 1;
3+
if (n < 0) {
4+
n = -n;
5+
x = 1 / x;
6+
}
7+
for (let i = n; i != 0; i = Math.floor(i / 2)) {
8+
if ((i & 1) == 1) {
9+
res *= x;
10+
}
11+
x *= x;
12+
}
13+
return res;
14+
};

0 commit comments

Comments
 (0)