[LC] 125. Valid Palindrome
2024-10-20 03:30:24
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:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
if s is '':
return True
import string
valid = set(string.ascii_letters + string.digits)
s_cp = s[:].lower()
left, right = 0, len(s) - 1
while left <= right:
if s_cp[left] not in valid:
left += 1
elif s_cp[right] not in valid:
right -= 1
elif s_cp[left] != s_cp[right]:
return False
else:
left += 1
right -= 1
return True
class Solution {
public boolean isPalindrome(String s) {
if (s.length() == 0) {
return true;
}
char[] charArr = s.toCharArray();
int left = 0, right = s.length() - 1;
while (left < right) {
if (!Character.isLetterOrDigit(charArr[left])) {
left += 1;
} else if (!Character.isLetterOrDigit(charArr[right])) {
right -= 1;
} else if (Character.toUpperCase(charArr[left]) != Character.toUpperCase(charArr[right])) { return false;
} else {
left += 1;
right -= 1;
}
}
return true;
}
}
最新文章
- 360急速浏览器BUG,POST表单提交参数丢失
- js问题解释
- iOS-XMPP客户端
- NSPredicate,谓词
- 关于java.sql.SQLRecoverableException: Closed Connection异常的解决方案(转)
- Swift编程语言学习4.1——周期
- angularjs中动态为audio绑定src
- 1、手把手教你Extjs5(一)搭建ExtJS5环境
- 一篇文章让你搞懂 SSL 证书
- C#快速入门
- 使用stringstream对象简化类型转换
- BZOJ.5305.[HAOI2018]苹果树(组合 计数)
- 浏览器兼容html头部<;meta>;标签主要内容详情
- 网络-01-端口号-linux端口详解大全
- VS2017无法进入安装界面问题的解决方法
- zabbix添加监控Mysql
- Linux基础学习(1)--Linux系统简介
- [LeetCode&;Python] Problem 599. Minimum Index Sum of Two Lists
- War3编辑器
- Linux中Postfix邮件认证配置(五)
热门文章
- Vue-router(1)之component标签
- 使用linux服务器安装wordpress博客详细教程
- Iptables的规则语法
- 【MySQL参数】-innodb_additional_mem_pool_size
- 01 语言基础+高级:1-2 面向对象和封装_day06【类与对象、封装、构造方法】
- node 第三方库总结
- ZJNU 2201 - 挖矿谷物语
- tensorflow object detection api android
- Filezilla Xshell SecureFX Win10等无法拖放文件(本地或线上)解决办法
- Office文档WEB端在线浏览(转换成Html)