当前位置:网站首页>leetcode/有效的回文串,含有不需要判断回文的字符
leetcode/有效的回文串,含有不需要判断回文的字符
2022-08-04 17:43:00 【xcrj】
代码
package com.xcrj;
/** * 剑指 Offer II 018. 有效的回文串,含有其他字符 * 给定一个字符串 s ,验证 s 是否是 回文串 ,只考虑字母和数字字符,可以忽略字母的大小写。 */
public class Solution18 {
/** * 双指针相向移动 */
public boolean isPalindrome1(String s) {
int l = 0, r = s.length() - 1;
// !!!双指针相向移动模板
while (l < r) {
while (l < r && !Character.isLetterOrDigit(s.charAt(l))) l++;
while (l < r && !Character.isLetterOrDigit(s.charAt(r))) r--;
if (l < r) {
if (Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r))) return false;
l++;
r--;
}
}
return true;
}
public static void main(String[] args) {
Solution18 solution18 = new Solution18();
System.out.println(solution18.isPalindrome1("A--a"));
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/XltzEq/solution/you-xiao-de-hui-wen-by-leetcode-solution-uj86/
来源:力扣(LeetCode)
边栏推荐
猜你喜欢
随机推荐
Liunx删除乱码文件
shell函数内如何调用另一个函数
The prefix and discretization
JWT主动校验Token是否过期
语音识别学习资源
R语言时间序列数据算术运算:使用diff函数计算时间序列数据的逐次差分、使用时间序列之间的除法计算相对变化率(乘以100获得百分比)
PT100铂热电阻三种测温方法介绍
Fork/Join框架
js函数传参是按值传递还是按引用传递?
《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
Thrift IDL Sample File
Boost library study notes (1) Installation and configuration
通关剑指 Offer——剑指 Offer II 010. 和为 k 的子数组
Codeforces积分系统介绍
RecyclerView 缓存与复用机制
最小区间覆盖
To eliminate asynchronous callbacks, it has to be async-await
【web自动化测试】Playwright快速入门,5分钟上手
OpenInfra Days China 2022|SelectDB与你共享 Apache Doris 在互联网广告业务中的实践
【Gazebo入门教程】第二讲 模型库导入与可视化机器人建模(模型编辑器)








