File tree Expand file tree Collapse file tree 2 files changed +97
-0
lines changed
Expand file tree Collapse file tree 2 files changed +97
-0
lines changed Original file line number Diff line number Diff line change 1+ <h1 style =" text-align : center ;" > <span style =" color : #00AF9B ;" >125. 验证回文串</span > </h1 >
2+
3+ ### 🚀 LeetCode
4+
5+ <base target =" _blank " >
6+
7+ <span style =" color : #00AF9B ;" >** Easy** </span > [ ** https://leetcode.cn/problems/valid-palindrome/ ** ] ( https://leetcode.cn/problems/valid-palindrome/ )
8+
9+ ---
10+
11+ ### ❓ Description
12+
13+ <br />
14+
15+ 如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,短语正着读和反着读都一样。
16+
17+ 则可以认为该短语是一个 ** 回文串** 。
18+
19+ 字母和数字都属于字母数字字符。
20+
21+ 给你一个字符串 ` s ` ,如果它是 ** 回文串** ,返回 ` true ` ;否则,返回 ` false ` 。
22+
23+ <br />
24+
25+ ** 示例 1:**
26+
27+ ```
28+ 输入: s = "A man, a plan, a canal: Panama"
29+ 输出: true
30+ 解释: "amanaplanacanalpanama" 是回文串
31+ ```
32+
33+ ** 示例 2:**
34+
35+ ```
36+ 输入: s = "race a car"
37+ 输出: false
38+ 解释: "raceacar" 不是回文串
39+ ```
40+
41+ ** 示例 3:**
42+
43+ ```
44+ 输入: s = " "
45+ 输出: true
46+ 解释: 在移除非字母数字字符之后, s 是一个空字符串 ""
47+ 由于空字符串正着反着读都一样, 所以是回文串
48+ ```
49+
50+ <br />
51+
52+ ** 提示:**
53+
54+ * ` 1 <= s.length <= 2 * 10^5 `
55+ * ` s ` 仅由可打印的 ` ASCII ` 字符组成
56+
57+ ---
58+
59+ ### ❗ Solution
60+
61+ <br />
62+
63+ #### idea
64+
65+ * 思路参考 [ ** 0009.回文数** ] ( ./0009.回文数.md )
66+
67+ <br />
68+
69+ #### Java
70+
71+ ```
72+ class Solution {
73+ public boolean isPalindrome(String s) {
74+ int left = 0;
75+ int right = s.length() - 1;
76+ while (left < right) {
77+ char l = s.charAt(left);
78+ char r = s.charAt(right);
79+ if (!Character.isLetter(l) && !Character.isDigit(l)) {
80+ left++;
81+ continue;
82+ }
83+ if (!Character.isLetter(r) && !Character.isDigit(r)) {
84+ right--;
85+ continue;
86+ }
87+ if (Character.toLowerCase(l) != Character.toLowerCase(r)) {
88+ return false;
89+ }
90+ left++;
91+ right--;
92+ }
93+ return true;
94+ }
95+ }
96+ ```
Original file line number Diff line number Diff line change 5151| <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 0121.买卖股票的最佳时机** ] ( ../easy/0121.买卖股票的最佳时机.md ) |
5252| <span style =" color : #FFB822 ;" >** Medium** </span > | [ ** 0122.买卖股票的最佳时机II** ] ( ../medium/0122.买卖股票的最佳时机II.md ) |
5353| <span style =" color : #FF2D55 ;" >** Hard** </span > | [ ** 0124.二叉树中的最大路径和** ] ( ../hard/0124.二叉树中的最大路径和.md ) |
54+ | <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 0125.验证回文串** ] ( ../easy/0125.验证回文串.md ) |
5455| <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 0136.只出现一次的数字** ] ( ../easy/0136.只出现一次的数字.md ) |
5556| <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 0141.环形链表** ] ( ../easy/0141.环形链表.md ) |
5657| <span style =" color : #00AF9B ;" >** Easy** </span > | [ ** 0144.二叉树的前序遍历** ] ( ../easy/0144.二叉树的前序遍历.md ) |
You can’t perform that action at this time.
0 commit comments