Skip to content

Commit 8a9d8b8

Browse files
committed
--update: 32-bit signed int reversal
1 parent dbd3dbd commit 8a9d8b8

File tree

2 files changed

+75
-14
lines changed

2 files changed

+75
-14
lines changed

src/_Problems_/reverse-number/index.js

+41
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,47 @@ function reverseNumber(num) {
1515
return reverse * Math.sign(num);
1616
}
1717

18+
/**
19+
*
20+
* Given a 32-bit signed integer, reverse digits of an integer.
21+
22+
Example 1:
23+
24+
Input: 123
25+
Output: 321
26+
Example 2:
27+
28+
Input: -123
29+
Output: -321
30+
Example 3:
31+
32+
Input: 1534236469
33+
Output: 0 // overflows
34+
Note:
35+
Assume we are dealing with an environment which could only
36+
store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1].
37+
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
38+
*/
39+
40+
function reverse32BitInt(x) {
41+
let isNegetive = 0;
42+
if (x < 0) {
43+
x *= -1;
44+
isNegetive = 1;
45+
}
46+
let reverse = 0;
47+
while (x >= 1) {
48+
const r = Math.floor(x % 10);
49+
reverse = reverse * 10 + r;
50+
x = Math.floor(x / 10);
51+
}
52+
if (reverse > 0x7fffffff) {
53+
return 0;
54+
}
55+
return isNegetive ? reverse * -1 : reverse;
56+
}
57+
1858
module.exports = {
1959
reverseNumber,
60+
reverse32BitInt
2061
};
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
1-
const { reverseNumber } = require('.');
1+
const { reverseNumber, reverse32BitInt } = require('.');
22

33
describe('Reverse Numbers', () => {
4-
it('Should return a number', () => {
5-
expect(typeof reverseNumber(1) === 'number');
6-
});
4+
describe('Normal Reverse', () => {
5+
it('Should return a number', () => {
6+
expect(typeof reverseNumber(1) === 'number');
7+
});
78

8-
it('Should reverse 45 to 54', () => {
9-
expect(reverseNumber(45)).toEqual(54);
10-
});
9+
it('Should reverse 45 to 54', () => {
10+
expect(reverseNumber(45)).toEqual(54);
11+
});
1112

12-
it('Should reverse -2 to -2', () => {
13-
expect(reverseNumber(-2)).toEqual(-2);
14-
});
13+
it('Should reverse -2 to -2', () => {
14+
expect(reverseNumber(-2)).toEqual(-2);
15+
});
16+
17+
it('Should reverse -1234567 to -7654321', () => {
18+
expect(reverseNumber(-1234567)).toEqual(-7654321);
19+
});
1520

16-
it('Should reverse -1234567 to -7654321', () => {
17-
expect(reverseNumber(-1234567)).toEqual(-7654321);
21+
it('Should throw error for invalid argument', () => {
22+
expect(() => reverseNumber('hello')).toThrow('Invalid Argument');
23+
});
1824
});
1925

20-
it('Should throw error for invalid argument', () => {
21-
expect(() => reverseNumber('hello')).toThrow('Invalid Argument');
26+
describe('32-bit signed integer reversal', () => {
27+
it('Should return a number', () => {
28+
expect(typeof reverse32BitInt(1) === 'number');
29+
});
30+
31+
it('Should reverse 123 to 321', () => {
32+
expect(reverse32BitInt(123)).toEqual(321);
33+
});
34+
35+
it('Should reverse -871 to -178', () => {
36+
expect(reverse32BitInt(-871)).toEqual(-178);
37+
});
38+
39+
it('Should return 0 for 1534236469 because of overflow when reversed', () => {
40+
expect(reverse32BitInt(1534236469)).toEqual(0);
41+
});
2242
});
2343
});

0 commit comments

Comments
 (0)