DEV Community

Naya Willis
Naya Willis

Posted on

1

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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️