Skip to content

Commit bff0c73

Browse files
authored
Merge pull request neetcode-gh#441 from Sinha1994/main
Solution for 7- Reverse Integer in TypeScript
2 parents 5515379 + b421501 commit bff0c73

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

typescript/7-Reverse-Integer.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const reverse = (x: number): number => {
2+
const max: number = 2 ** 31 - 1;
3+
const min: number = -(2 ** 31);
4+
5+
let result: number = 0;
6+
while (x !== 0) {
7+
const digit = x % 10;
8+
x = Math.trunc(x / 10);
9+
10+
if (result > max / 10 || (result === max / 10 && digit >= max % 10)) {
11+
return 0;
12+
} else if (
13+
result < min / 10 ||
14+
(result === max / 10 && digit <= min % 10)
15+
) {
16+
return 0;
17+
} else {
18+
result = result * 10 + digit;
19+
}
20+
}
21+
22+
return result;
23+
};

0 commit comments

Comments
 (0)