Skip to content

Commit 4efeca9

Browse files
committed
✨ [q7] First attemp
1 parent d0dd95d commit 4efeca9

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

7/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
https://leetcode.com/problems/reverse-integer/
2+
3+
# Question
4+
5+
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`.
6+
7+
**Assume the environment does not allow you to store 64-bit integers (signed or unsigned).**
8+
9+
**Example 1:**
10+
11+
```
12+
Input: x = 123
13+
Output: 321
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: x = -123
20+
Output: -321
21+
```
22+
23+
**Example 3:**
24+
25+
```
26+
Input: x = 120
27+
Output: 21
28+
```
29+
30+
**Constraints:**
31+
32+
- `231 <= x <= 231 - 1`

7/my_solution.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number} x
3+
* @return {number}
4+
*/
5+
const reverse = (x) => {
6+
let charList = x.toString().split(''), result = null;
7+
// charList.forEach((char, i) => {
8+
// console.log("char")
9+
// console.log(char)
10+
// console.log(i)
11+
// console.log("parseInt(char[i]) * i")
12+
// console.log(parseInt(char[i]) * i)
13+
// result += parseInt(char[i]) * i;
14+
// i *= 10;
15+
// })
16+
17+
result = reverseCharList(charList, 0, 1, null);
18+
19+
console.log("result")
20+
console.log(result)
21+
};
22+
23+
const reverseCharList = (charList, i, factor, result) => {
24+
if (undefined === charList[i]) return result;
25+
console.log("charList[i]")
26+
console.log(charList[i])
27+
28+
result += (parseInt(charList[i]) * factor);
29+
factor *= 10;
30+
console.log("result")
31+
console.log(result)
32+
console.log("factor")
33+
console.log(factor)
34+
35+
i++;
36+
return reverseCharList(charList, i, factor, result);
37+
}
38+
39+
40+
// reverse(321)
41+
reverse(120)

7/solution.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} x
3+
* @return {number}
4+
*/
5+
const reverse = (x) => {
6+
return reverseCharList(x.toString().split(''), 0, 1, null);
7+
};
8+
9+
const reverseCharList = (charList, i, factor, result) => {
10+
if (undefined === charList[i]) return result;
11+
12+
result += (parseInt(charList[i]) * factor);
13+
factor *= 10;
14+
i++;
15+
16+
return reverseCharList(charList, i, factor, result);
17+
}
18+

0 commit comments

Comments
 (0)