8000 Update README.md · abroroo/leetcode@03f59e1 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 03f59e1

Browse files
authored
Update README.md
1 parent 7a8c84f commit 03f59e1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ List is periodically updated
55
## LeetCode
66

77
### #7 Reverse Integer
8+
9+
Using Math formulas to achieve the result
10+
11+
```
12+
var reverse = function(x) {
13+
let result = 0;
14+
// until x is zero
15+
while (x != 0){
16+
// add last digit of x derived as(x % 10) to the end of result as (num * 10) + anyNum
17+
result = (result * 10) + (x % 10)
18+
19+
// remove the last digit of x from x using formula below
20+
x = (x - (x % 10))/10
21+
}
22+
23+
// check if within the range [-2^31, 2^31 -1] before returning result
24+
if (result <= -Math.pow(2, 31) || result >= Math.pow(2, 31) - 1) return 0
25+
return result
26+
};
27+
```
28+
By converting to string, less optimal
829
```
930
var reverse = function(x) {
1031
// get the absolute value(non negative) and convert to String

0 commit comments

Comments
 (0)
0