[LeetCode] reverse (easy)

Hwanhoon KIM·2023년 5월 24일
0

Leetcode

목록 보기
3/4

이번 글은 영어 공부도 할 겸 영어로 써야겠다.

7. Reverse Integer

Qustion.

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Example 1:

Input: x = 123
Output: 321

Example 2:

Input: x = -123
Output: -321

Example 3:

Input: x = 120
Output: 21

Constraints:

-2312^{31} <= x <= 23112^{31}-1

My code:

1  function reverse(x) {
2    const y = String(x)
3    const z = y.split('')
4    let d;
5    if (z[0] === '-') {
6        z.shift()
7        const a = z.reverse()
8        a.splice(0,0,'-')
9        const b = String(a)
10        const c = b.replaceAll(',', '')
11        d = Number(c)
12    } else {
13        const a = z.reverse()
14        const b = String(a)
15        const c = b.replaceAll(',', '')
16        d = Number(c)
17    }
18    if (d <= -(2**31) || d >= 2**31 - 1) {
19        console.log('rejected')
20        return 0
21    } else {
22        console.log(d)
23        return d
24    }
25 }

The process may seem a bit intricate at first glance, but allow me to explain it in a more refined manner. 😎

To begin with, I transformed the parameter x into an array, where each element represents a single digit. To achieve this, I converted x into a string using String(x), and then employed the .split('') method.

Let's take the value of x as -12345. After the split, the array becomes ['-', 1, 2, 3, 4, 5]. I assigned this array to d, which represents the resulting number. So, you can visualize d as the final outcome.

For negative numbers, the array should begin with a - sign. Hence, I implemented an if statement (if (z[0] === '-')) and utilized .shift() to remove the - symbol temporarily, allowing further manipulations.

To reverse the array, I employed .reverse(). Then, by utilizing .splice(0, 0, '-'), I placed the - sign back at the beginning of the array.

Afterwards, I transformed the array back into a string, resulting in -54321. By employing the Number() function, I successfully converted it back into a numerical value. Voilà! The task is complete.

The same process applies to positive numbers, excluding the need to remove and reinsert the - sign.

Finally, I implemented an if statement to ensure that the condition of -2312^{31} <= x <= 23112^{31}-1 is satisfied. Ultimately, I returned the result accordingly.

My initial encounter with a LeetCode problem was quite challenging and consumed a significant amount of time. Surprisingly, it was classified as an "easy" level problem. However, the one I solved swiftly within 20 minutes was classified as "medium" difficulty. This accomplishment has left me with a sense of achievement. To further enhance my skills, I aim to tackle at least two questions daily. Let's maintain this momentum and continue to progress!

profile
Fullstack Developer, I post about HTML, CSS(SASS, LESS), JavaScript, React, Next, TypeScript.

0개의 댓글