Skip to content

Commit e1134dd

Browse files
committed
feat: add the solution of Valid Palindrome(125) with kotlin.
1 parent f7f1724 commit e1134dd

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
| [119][119-question] | [Pascal's Triangle II][119-tips] | [Easy][E] | [][119-java] | | [][119-kotlin] |
8686
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [Easy][E] | [][121-java] | | [][121-kotlin] |
8787
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [Easy][E] | [][122-java] | | [][122-kotlin] |
88+
| [125][125-question] | [Valid Palindrome][125-tips] | [Easy][E] | | | [][125-kotlin] |
8889
| [226][226-question] | [Invert Binary Tree][226-tips] | [Easy][E] | [][226-java] | [][226-js] | [][226-kotlin] |
8990
| [504][504-question] | [Base 7][504-tips] | [Easy][E] | | | [][504-kotlin] |
9091
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [Easy][E] | [][543-java] | | [][543-kotlin] |
@@ -204,6 +205,7 @@
204205
[119-question]: https://leetcode.com/problems/pascals-triangle-ii/
205206
[121-question]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
206207
[122-question]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
208+
[125-question]: https://leetcode.com/problems/valid-palindrome/
207209
[226-question]: https://leetcode.com/problems/invert-binary-tree/
208210
[504-question]: https://leetcode.com/problems/base-7/description/
209211
[543-question]: https://leetcode.com/problems/diameter-of-binary-tree/
@@ -304,6 +306,7 @@
304306
[119-tips]: ./tips/119/README.md
305307
[121-tips]: ./tips/121/README.md
306308
[122-tips]: ./tips/122/README.md
309+
[125-tips]: ./tips/125/README.md
307310
[226-tips]: ./tips/226/README.md
308311
[504-tips]: ./tips/504/README.md
309312
[543-tips]: ./tips/543/README.md
@@ -525,6 +528,7 @@
525528
[119-kotlin]: ./src/_119/kotlin/Solution.kt
526529
[121-kotlin]: ./src/_121/kotlin/Solution.kt
527530
[122-kotlin]: ./src/_122/kotlin/Solution.kt
531+
[125-kotlin]: ./src/_125/kotlin/Solution.kt
528532
[226-kotlin]: ./src/_226/kotlin/Solution.kt
529533
[504-kotlin]: ./src/_504/kotlin/Solution.kt
530534
[543-kotlin]: ./src/_543/kotlin/Solution.kt

src/_125/kotlin/Solution.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package _125.kotlin
2+
3+
/**
4+
* @author relish
5+
* @since 2018/11/09
6+
*/
7+
class Solution {
8+
fun isPalindrome(s: String): Boolean {
9+
if (s == " ") return true
10+
val len = s.length
11+
var i = 0
12+
var j = len - 1
13+
while (i <= j) {
14+
try {
15+
while (!(s[i] in 'a'..'z' || s[i] in 'A'..'Z' || s[i] in '0'..'9')) i++
16+
while (!(s[j] in 'a'..'z' || s[j] in 'A'..'Z' || s[j] in '0'..'9')) j--
17+
} catch (e: Exception) {
18+
return true
19+
}
20+
try {
21+
if (!s[i++].equals(s[j--], true)) return false
22+
} catch (e: Exception) {
23+
return false
24+
}
25+
}
26+
return true
27+
}
28+
}
29+
30+
fun main(args: Array<String>) {
31+
println(Solution().isPalindrome("A man, a plan, a canal: Panama"))
32+
println(Solution().isPalindrome("race a car"))
33+
println(Solution().isPalindrome(" "))
34+
println(Solution().isPalindrome("."))
35+
println(Solution().isPalindrome("0P"))
36+
println(Solution().isPalindrome("ab2a"))
37+
}

tips/125/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[125. Valid Palindrome][title]
2+
3+
## Description
4+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
5+
6+
7+
**Example 1:**
8+
9+
```
10+
Input: "A man, a plan, a canal: Panama"
11+
Output: true
12+
```
13+
**Example 2:**
14+
15+
```
16+
Input: "race a car"
17+
Output: false
18+
```
19+
20+
**Note:**
21+
For the purpose of this problem, we define empty string as valid palindrome.
22+
23+
**Tags:**
24+
`Two Pointers``String`
25+
26+
27+
28+
## 思路 1
29+
1 空串为true
30+
2 从两边往中间靠拢, 遇到不是字母也不是数字的就跳过
31+
3 左边游标索引大于右边游标索引则结束遍历
32+
4 遍历过程中一旦发生不相等, 则不是回文
33+
kotlin(228ms/94.44%):
34+
```kotlin
35+
class Solution {
36+
fun isPalindrome(s: String): Boolean {
37+
if (s == " ") return true
38+
val len = s.length
39+
var i = 0
40+
var j = len - 1
41+
while (i <= j) {
42+
try {
43+
while (!(s[i] in 'a'..'z' || s[i] in 'A'..'Z' || s[i] in '0'..'9')) i++
44+
while (!(s[j] in 'a'..'z' || s[j] in 'A'..'Z' || s[j] in '0'..'9')) j--
45+
} catch (e: Exception) {
46+
return true
47+
}
48+
try {
49+
if (!s[i++].equals(s[j--], true)) return false
50+
} catch (e: Exception) {
51+
return false
52+
}
53+
}
54+
return true
55+
}
56+
}
57+
```
58+
59+
## 结语
60+
61+
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
62+
63+
[title]: https://leetcode.com/problems/valid-palindrome/
64+
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)