You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solutions/reverse_integer.js
+72-15Lines changed: 72 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,6 @@
2
2
3
3
Given a 32-bit signed integer, reverse digits of an integer.
4
4
5
-
Example 1:
6
-
7
5
Input: 123
8
6
Output: 321
9
7
Example 2:
@@ -15,41 +13,100 @@ Example 3:
15
13
Input: 120
16
14
Output: 21
17
15
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
19
19
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.
//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.
29
35
varreverseBest=function(x){
30
36
vary=Math.abs(x);
31
37
varresult=0;
32
38
33
-
while(y!==0){
34
-
varresult=(result*10)+y%10;
39
+
while(y){
40
+
varresult=(result*10)+y%10;
35
41
y=parseInt(y/10);
36
42
// console.log(y);
37
43
}
38
44
39
45
40
46
x>0 ? result=result : result=-result;
41
-
if(result>2147483648||result<-2147483648)return0;
42
-
returnresult
43
-
47
+
if(result>2147483648||result<-2147483648)return0;
48
+
returnresult;
44
49
}
45
50
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
47
58
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
50
66
51
67
So, after the first execution of the iteration, the value of result will be like below
68
+
52
69
0 + 3
53
70
(3 * 10) + 2
54
71
(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
+
constreverseNum=x=>{
94
+
95
+
letresult=0;
96
+
97
+
if(x<0){
98
+
// For this case, slice the string after the first "-" sign. Else I will get a NaN
0 commit comments