Skip to content

Commit cc9a438

Browse files
committed
feature: zigzag-conversion
1 parent fddb9d6 commit cc9a438

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

code/source/PalindromeNumber.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class PalindromeNumber {
2+
3+
public boolean isPalindrome(int x) {
4+
if (x < 0 || x != 0 && x % 10 == 0) {
5+
return false;
6+
}
7+
int num = 0;
8+
while (x > num) {
9+
num = num * 10 + x % 10;
10+
x = x / 10;
11+
}
12+
return x == num || num / 10 == x;
13+
}
14+
}

solution/leetcode/9.回文数.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 9. 回文数
2+
3+
## 日期
4+
5+
## 题目描述
6+
7+
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
8+
9+
示例:
10+
11+
```
12+
示例 1:
13+
14+
输入: 121
15+
输出: true
16+
17+
示例 2:
18+
19+
输入: -121
20+
输出: false
21+
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
22+
23+
示例 3:
24+
25+
输入: 10
26+
输出: false
27+
解释: 从右向左读, 为 01 。因此它不是一个回文数。
28+
```
29+
30+
进阶:
31+
32+
你能不将整数转为字符串来解决这个问题吗?
33+
34+
## 想法
35+
36+
1. 反转整数,比较
37+
2. 直接把数字拆成两半比较
38+
39+
## My
40+
41+
解法2:
42+
43+
```
44+
public class PalindromeNumber {
45+
46+
public boolean isPalindrome(int x) {
47+
if (x < 0 || x != 0 && x % 10 == 0) {
48+
return false;
49+
}
50+
int num = 0;
51+
while (x > num) {
52+
num = num * 10 + x % 10;
53+
x = x / 10;
54+
}
55+
return x == num || num / 10 == x;
56+
}
57+
}
58+
```

0 commit comments

Comments
 (0)