Skip to content

Commit 2699c93

Browse files
committed
reverse num
1 parent 2bb7a3d commit 2699c93

File tree

1 file changed

+72
-15
lines changed

1 file changed

+72
-15
lines changed

solutions/reverse_integer.js

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
Given a 32-bit signed integer, reverse digits of an integer.
44
5-
Example 1:
6-
75
Input: 123
86
Output: 321
97
Example 2:
@@ -15,41 +13,100 @@ Example 3:
1513
Input: 120
1614
Output: 21
1715
Note:
18-
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.*/
16+
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
17+
18+
'Overflows' here means the resultant number is more than the greatest number that can be handled in JS or less than the min number that can be handled in js
1919
20-
// My solution
20+
The upper bound of a signed integer is not 2^32 - 1, but 2^31 - 1, since the first bit is the sign bit.
21+
And lower bound is -(2^31 - 1)
22+
*/
23+
24+
// SOLUTION-1 My solution
2125
var reverse = function(x) {
2226
var reversedX = +Math.abs(x).toString().split('').reverse().join('');
2327
return reversedX > 2147483647 ? 0 : x < 0 ? -reversedX : reversedX;
2428
}
2529

26-
// console.log(reverse(123));
30+
// console.log(reverse(123)); // => 321
31+
32+
// console.log(reverse(-123)); // => -321
2733

28-
//Best Performing solution. And also if the Problems asks for not to use any string related methods.
34+
// // SOLUTION-2 - Best Performing solution. And also if the Problems asks for not to use any string related methods.
2935
var reverseBest = function(x) {
3036
var y = Math.abs(x);
3137
var result = 0;
3238

33-
while (y !== 0) {
34-
var result = (result * 10) + y % 10;
39+
while (y) {
40+
var result = (result * 10) + y % 10;
3541
y = parseInt(y / 10);
3642
// console.log(y);
3743
}
3844

3945

4046
x > 0 ? result = result : result = -result;
41-
if (result > 2147483648 || result < -2147483648) return 0;
42-
return result
43-
47+
if (result > 2147483648 || result < -2147483648) return 0;
48+
return result;
4449
}
4550

46-
console.log(reverseBest(123));
51+
// console.log(reverseBest(123)); // => 321
52+
// console.log(reverseBest(-123)); // => -321
53+
// console.log(reverseBest( 1534236469 )) // => 0
54+
55+
/*My note on the above best solution -
56+
57+
FIRST - I have to pick up the last digit and bring it forward
4758
48-
/*My note on the above best solution - The mod will return the remainder, i.e. the last digit for each iteration, starting with the last digit then the last to last and so on. So, in case of x - 123, after first iteration y % 10 will return 3, then 2, then 1
49-
And then in the parseInt(y/10) will consecutively return 12, 1 and then 0
59+
So, I use modulo operator to hook to the last digit. The mod will return the remainder, i.e. the last digit for each iteration, starting with the last digit then the last to last and so on.
60+
61+
So, in case of x = 123, after first iteration y % 10 will return 3, then 2, then 1
62+
63+
SECOND - And then, I need to extract the rest of the digits.
64+
65+
So I do, parseInt(y/10), which will always return an integer leaving out the last decimal positions. So for for my case of 123 ( parseInt(123/10) and then parseInt(12/10) and then parseInt(1/10)) will consecutively return 12, 1 and then 0
5066
5167
So, after the first execution of the iteration, the value of result will be like below
68+
5269
0 + 3
5370
(3 * 10) + 2
5471
(32 * 10 ) + 1
55-
*/
72+
73+
74+
****************************************
75+
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems.
76+
77+
parseInt(string, radix);
78+
79+
string - The value to parse. If the string argument is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string argument is ignored.
80+
81+
radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string.
82+
83+
Return value - An integer number parsed from the given string. If the first character cannot be converted to a number, NaN is returned.
84+
85+
parseInt(15.99, 10); // => 15
86+
87+
parseInt('15,123', 10); // => 15
88+
89+
*/
90+
91+
// SOLUTION-3 - Almost similar to SOL-1 except using toString() but anyway converting to string with '
92+
93+
const reverseNum = x => {
94+
95+
let result = 0;
96+
97+
if (x < 0) {
98+
// For this case, slice the string after the first "-" sign. Else I will get a NaN
99+
result = -(('' + x).slice(1).split('').reverse().join(''));
100+
} else {
101+
result = +(('' + x).split('').reverse().join(''));
102+
}
103+
104+
// In split() and join() I have to pass the delimiter of an empty string as '' - Else tit will be an error.
105+
106+
return (result < (-Math.pow(2,31)) || result > (Math.pow(2,31) -1) ) ? 0 : result
107+
// Note the syntax above for the ternary operator above as compared to the SOL-1 and 2. Here, I am using just a single return statement. And
108+
}
109+
110+
console.log(reverseNum(123)); // => 321
111+
console.log(reverseNum(-123)); // => -321
112+
console.log(reverseNum( 1534236469 )) // => 0

0 commit comments

Comments
 (0)