125:验证回文串
This commit is contained in:
parent
0d82f8b31c
commit
3d8f37136e
70
src/main/java/leetcode/editor/cn/ValidPalindrome.java
Normal file
70
src/main/java/leetcode/editor/cn/ValidPalindrome.java
Normal file
@ -0,0 +1,70 @@
|
||||
//给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
|
||||
//
|
||||
// 说明:本题中,我们将空字符串定义为有效的回文串。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入: "A man, a plan, a canal: Panama"
|
||||
//输出: true
|
||||
//解释:"amanaplanacanalpanama" 是回文串
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入: "race a car"
|
||||
//输出: false
|
||||
//解释:"raceacar" 不是回文串
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= s.length <= 2 * 10⁵
|
||||
// 字符串 s 由 ASCII 字符组成
|
||||
//
|
||||
// Related Topics 双指针 字符串 👍 492 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//125:验证回文串
|
||||
public class ValidPalindrome {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new ValidPalindrome().new Solution();
|
||||
solution.isPalindrome("A man, a plan, a canal: Panama");
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean isPalindrome(String s) {
|
||||
s = s.toUpperCase();
|
||||
int start = 0;
|
||||
int end = s.length() - 1;
|
||||
while (start < end) {
|
||||
char st = s.charAt(start);
|
||||
if (!((st >= 'A' && st <= 'Z') || (st >= '0' && st <= '9'))) {
|
||||
start++;
|
||||
continue;
|
||||
}
|
||||
char se = s.charAt(end);
|
||||
if (!((se >= 'A' && se <= 'Z') || (se >= '0' && se <= '9'))) {
|
||||
end--;
|
||||
continue;
|
||||
}
|
||||
if (st != se) {
|
||||
return false;
|
||||
}
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<p>给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。</p>
|
||||
|
||||
<p><strong>说明:</strong>本题中,我们将空字符串定义为有效的回文串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> "A man, a plan, a canal: Panama"
|
||||
<strong>输出:</strong> true
|
||||
<strong>解释:</strong>"amanaplanacanalpanama" 是回文串
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> "race a car"
|
||||
<strong>输出:</strong> false
|
||||
<strong>解释:</strong>"raceacar" 不是回文串
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li>字符串 <code>s</code> 由 ASCII 字符组成</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>双指针</li><li>字符串</li></div></div><br><div><li>👍 492</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user