Skip to content

Commit a2aa65e

Browse files
committed
Add: Add question 9
1 parent 7d1efb0 commit a2aa65e

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed

2485-Find the Pivot Integer/Note.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ It is guaranteed that there will be at most one pivot index for the given input.
1212

1313
- `1 <= n <= 1000`
1414

15-
# 基礎思路
15+
## 基礎思路
1616

1717
本題要求尋找一個整數 `x`,使得:
1818

9-Palindrome Number/Note.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 9. Palindrome Number
2+
3+
Given an integer `x`, return `true` if `x` is a palindrome, and `false` otherwise.
4+
5+
**Constraints:**
6+
7+
- `-2^31 <= x <= 2^31 - 1`
8+
9+
## 基礎思路
10+
11+
本題要求判斷整數 `x` 是否為回文數。
12+
回文概念為「從左讀與從右讀相同」,因此若將整數反轉後與原值相等,該整數即為回文。
13+
14+
在正式設計解法前,我們需要注意幾個核心觀察:
15+
16+
1. **負數必定不是回文**
17+
因為負號 `-` 會出現在最左側,但反轉後會跑到最右側,因此永不相等。
18+
19+
2. **反轉整數即可直接比對**
20+
只要將輸入的整數逐位反轉(利用取餘數與整數除法),最後比對是否與原值相同。
21+
22+
3. **不需使用字串轉換**
23+
題目允許純數值操作,因此使用取餘數 `%`、整數除法即可完成。
24+
25+
4. **反轉過程使用 while 逐位取 digit**
26+
反覆將最低位取出、累積到反轉值中,直到整數變為 0。
27+
28+
基於以上觀察即可完成高效、純數值的判斷流程。
29+
30+
## 解題步驟
31+
32+
### Step 1:處理負數與保留原始值
33+
34+
首先排除負數(負數必定不是回文),
35+
並將原始值保存起來以便最後比對。
36+
37+
```typescript
38+
// 負數因為有負號,無法是回文數
39+
if (x < 0) {
40+
return false;
41+
}
42+
43+
// 保存原始值,用於最終比較
44+
const originalValue = x;
45+
```
46+
47+
### Step 2:宣告反轉累積變數 reversedValue
48+
49+
反轉結果會使用一個累積變數 `reversedValue`,初始為 0。
50+
51+
```typescript
52+
// reversedValue 負責累加反轉後的數字
53+
let reversedValue = 0;
54+
```
55+
56+
### Step 3:主迴圈 — 逐位取出數字並反轉
57+
58+
使用 while 迴圈反覆執行「取最低位 → 加入反轉值 → 去除最低位」的操作。
59+
60+
```typescript
61+
// 反轉所有位數(使用取餘數與快速整數除法)
62+
while (x > 0) {
63+
// 取出最低有效位數
64+
const digit = x % 10;
65+
66+
// 將此位數加入反轉結果中
67+
reversedValue = (reversedValue * 10) + digit;
68+
69+
// 以 32 位整數截斷方式移除最低位
70+
x = (x / 10) | 0;
71+
}
72+
```
73+
74+
### Step 4:比較反轉結果與原始值
75+
76+
若反轉後與原始值相等,即為回文。
77+
78+
```typescript
79+
// 回文的定義:反轉後仍與原值相同
80+
return reversedValue === originalValue;
81+
```
82+
83+
## 時間複雜度
84+
85+
- while 迴圈會執行一次 per digit,對於整數最多約 10 位。
86+
- 總時間複雜度為 $O(d)$,其中 $d$ 為數字位數(最大為 10)。
87+
88+
> $O(d)$
89+
90+
## 空間複雜度
91+
92+
- 僅使用常數額外變數 `reversedValue``originalValue``digit`
93+
- 總空間複雜度為 $O(1)$。
94+
95+
> $O(1)$

9-Palindrome Number/answer.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function isPalindrome(x: number): boolean {
2+
// Negative numbers cannot be palindromes because of the minus sign
3+
if (x < 0) {
4+
return false;
5+
}
6+
7+
// Preserve the original value for final comparison
8+
const originalValue = x;
9+
10+
// reversedValue accumulates the reversed digits of x
11+
let reversedValue = 0;
12+
13+
// Reverse all digits using modulo and fast integer division
14+
while (x > 0) {
15+
// Extract the least significant digit
16+
const digit = x % 10;
17+
18+
// Append the digit to the reversed value
19+
reversedValue = (reversedValue * 10) + digit;
20+
21+
// Remove the least significant digit using 32-bit truncation
22+
x = (x / 10) | 0;
23+
}
24+
25+
// A palindrome must equal its reversed representation
26+
return reversedValue === originalValue;
27+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function isPalindrome(x: number): boolean {
2+
3+
}

0 commit comments

Comments
 (0)