/LeetCode-7-Reverse-An-Integer

My solution to LeetCode #7

Primary LanguageJavaScript

LeetCode-7-Reverse-An-Integer

My solution to LeetCode #7

I've approached this problem with the intention of optimising for time complexity not space complexity. As such I've avoided the common method chaining solution, where the input is converted into a string and string methods performed on it.

My understanding of this problem revolves around treating the integer like an array and trying to perform array like "methods" on it using just mathematical operators. Specifically, we are going to create pop() and push() like operations using the following code.

Pop()

  1. let popped = input % 10;

    • This line will take any given input and store in the popped variable it's last digit.

    EXAMPLE:

    popped = 123 % 10; In this case popped will be 3 as the modulus operator (%) divides our left operand by the right operand and returns the remainder. Which in this case will be stored in popped as 3.

  2. input = Math.floor(n/10);

    • This line is relatively simple. Having stored the last digit of our given input we now want to remove it.

    EXAMPLE:

    input = MATH.floor(123/10);

    In this case input === 12. As 123/10 = 12.3 then Math.floor truncates the decimal place.

Push()

Now we are going to create a push() like operation and push() our stored variable onto our reverseN variable.

let reverseN = 0; reverseN = reverseN * 10 + popped

To understand the above line better we have to remember that our two "methods" defined above are surround by a while loop

So, on the each pass of our loop in our example where the input is 123

1st pass) reverseN will store 3 because (0 * 10) + 3 = 3

2nd pass) reverseN will store 32 because (3 * 10) + 2 = 32

3rd pass) reverseN will store 321 because (32* 10) + 1 = 321

These two operations and our while loop essentially solve the problem. There are some edge cases that need to be handled in order to pass all the tests but they are fairly self explanatory.

Our time complexity is O (n) and our space complexity simplifies to O(1)