DEV Community

Naya Willis
Naya Willis

Posted on

Leetcode: Reverse Integer

Today I will be walking through the Reverse Integer Leetcode coding challenge.

Instructions

Given a 32-bit signed integer, reverse digits of an integer.

Note:
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Overview

const reverseInteger = num => { // CODE GOES HERE } 
Enter fullscreen mode Exit fullscreen mode

Example 1

Input: x = -4253 Output: -3524 
Enter fullscreen mode Exit fullscreen mode

Example 2

Input: x = 721 Output: 127 
Enter fullscreen mode Exit fullscreen mode

Let's Play

Step 1

There were 2 exponent expressions that I evaluated first. 1 - (-2 ^ 31) and 2 - (2 ^ 31 - 1). I used the Math module to handle those exponent expressions.

let startPoint = Math.pow(-2, 31) let endPoint = (Math.pow(-2, 31) - 1) 
Enter fullscreen mode Exit fullscreen mode

Step 2

I converted the passed in number into a sting using the toString() method.

let numToString = num.toString() 
Enter fullscreen mode Exit fullscreen mode

Step 3

Split the string, reversed it, and then joined it back together.

let numSplitReversedJoined = numToString.split('').reverse().join('') 
Enter fullscreen mode Exit fullscreen mode

Step 4

Check if there is a hyphen at the beginning of the string, split the string, then use the shift method to remove and return the hyphen. Otherwise, we do nothing.

let hyphen = numToString.includes('-') ? numToString.split('').shift() : null 
Enter fullscreen mode Exit fullscreen mode

Step 5

If there is a hyphen, concatenate it to the numToString then parsing it back into a number

let numValueWithHyphen = parseInt(hyphen + (numSplitReversedJoined)) let numValueWOHyphen = parseInt(numSplitReversedJoined) 
Enter fullscreen mode Exit fullscreen mode

Step 6

Return either the number if it has a hyphen, or the number without it.

let numValue = hyphen !== null ? numValueWithHyphen : numValueWOHyphen 
Enter fullscreen mode Exit fullscreen mode

Step 7

Lastly, check if the number reversed is within range of the startPoint and endPoint that we defined in the beginning. If it's out of range I return 0, otherwise I return the number.

if (numValue <= startPoint || numValue >= endPoint || numValue === 0) { return 0 } return xValue 
Enter fullscreen mode Exit fullscreen mode

All together

const reverseInteger = num => { let startPoint = Math.pow(-2, 31) let endPoint = (Math.pow(-2, 31) - 1) let numToString = num.toString() let numSplitReversedJoined = numToString.split('').reverse().join('') let hyphen = numToString.includes('-') ? numToString.split('').shift() : null let numValueWithHyphen = parseInt(hyphen + (numSplitReversedJoined)) let numValueWOHyphen = parseInt(numSplitReversedJoined) let numValue = hyphen !== null ? numValueWithHyphen : numValueWOHyphen if (numValue <= startPoint || numValue >= endPoint || numValue === 0) { return 0 } return numValue } 
Enter fullscreen mode Exit fullscreen mode

And, there we have it.

Top comments (0)