# Solution by hash9

• #### Approach #1 Convert the number into array of strings

Intuition

The first idea that comes to mind is to simply revert the number itself, but since we will also be dealing with negative numbers, reversing the number without considering the hyphen ['-'] will lead to a terrible resulting number.

So the second idea would be converting the number into an array of strings, which will allow us to position any type of element anywhere in the array, resolving our obstacle from the first idea.

Following the thoughts based on second idea and also considering the fact that the environment could only store integers within the 32-bit signed integer range, let us consider an example.

For example, if the input is `-987654321` we will first convert this number into an array of strings which will be `["-","9","8","7","6","5","4","3","2","1"]` and after applying few array prototypes to solve the problem, we can then check the resulting number for strict environment.

Algorithm

First of all, lets convert the number into an array of strings, which may or may not contain ['-'], If the array contains ['-'], then do not consider that element and shift the focus to numeric elements. Since now all the elements are numeric we can reverse the elements with `reverse()` prototype and then finally convert it back to number using `parseInt()` prototype.
Now for checking whether the reversed number falls outside the strict environment we can use `Math.pow()` method and return 0 if true.

Javascript

``````var reverse = function(x) {
let arrayOfString= x.toString().split('')
let result;
if(arrayOfString[0] == '-') {
let arrayOfStringWithoutHyphen = arrayOfString.shift()
result = parseInt(arrayOfStringWithoutHyphen +arrayOfString.reverse().join(''))
} else {
result = parseInt(arrayOfString.reverse().join(''))
}
if(Math.abs(result) > Math.pow(2, 31)) {
result = 0;
}
return result;
};
``````

Complexity Analysis

• Time complexity : O(1).
• Space complexity : O(1).

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.