题目来源:https://leetcode.com/problems/valid-palindrome/
题目难度:Easy
题目
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
1 | Input: "A man, a plan, a canal: Panama" |
Example 2:
1 | Input: "race a car" |
分析
只考虑字母和数字。忽略大小写。
解答1[Java]:
1 | class Solution { |
思路
先打一个表。
注意:通过 charMap['A']
这种方式访问数组元素的时候,Java 会自动把 []
中的字符转换为 int
型。
解答2[Java]:
1 | public class Solution { |
使用正则表达式和 Java 的类库。先把所有字母都转换成小写,反转字符串然后对比。
其他
发现 Java 的连等赋值好像比非连等赋值性能要更好一些。