[LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
验证一个给定的字符串是否为回文,用两个指针分别指向字符的首尾,判断是否相同,相同就的都向中间移动1位,判断下一组,左指针小于右指针就一直循环。遇到标点符号就跳过,处理下一个。遇到大写字母就转换成小写字母。
Java:
class Solution {
public boolean isPalindrome(String s) {
char[] chs = s.toCharArray();
int left = 0, right = s.length() - 1;
while (left <= right) {
while (left < right && !Character.isLetterOrDigit(chs[left]))
left++;
while (left < right && !Character.isLetterOrDigit(chs[right]))
right--;
if (Character.toLowerCase(chs[left++]) != Character.toLowerCase(chs[right--]))
return false;
}
return true;
}
}
Java:
public class Solution {
public boolean isPalindrome(String s) {
int size = s.length(), i = 0, j = size - 1;
s = s.toLowerCase(); while (i < j) {
if (!(s.charAt(i) >= 'a' && s.charAt(i) <= 'z') && !(s.charAt(i) >= '0' && s.charAt(i) <= '9')) {
i++;
}
else if (!(s.charAt(j) >= 'a' && s.charAt(j) <= 'z') && !(s.charAt(j) >= '0' && s.charAt(j) <= '9')) {
j--;
}
else {
if (s.charAt(i) != s.charAt(j)) return false; i++;
j--;
}
}
return true;
}
}
Python:
class Solution:
def isPalindrome(self, s):
i, j = 0, len(s) - 1
while i < j:
while i < j and not s[i].isalnum():
i += 1
while i < j and not s[j].isalnum():
j -= 1
if s[i].lower() != s[j].lower():
return False
i, j = i + 1, j - 1
return True
C++:
class Solution {
public:
bool isPalindrome(string s) {
int left = 0, right = s.size() - 1 ;
while (left < right) {
if (!isalnum(s[left])) ++left;
else if (!isalnum(s[right])) --right;
else if ((s[left] + 32 - 'a') %32 != (s[right] + 32 - 'a') % 32) return false;
else {
++left; --right;
}
}
return true;
}
};
C++:
class Solution {
public:
bool isPalindrome(string s) {
int l = 0, r = s.size() - 1;
while(l <= r){
while(!isalnum(s[l]) && l < r) l++;
while(!isalnum(s[r]) && l < r) r--;
if(toupper(s[l]) != toupper(s[r])) return false;
l++, r--;
}
return true;
}
};
类似题目:
[LeetCode] 9. Palindrome Number 验证回文数字
[LeetCode] 5. Longest Palindromic Substring 最长回文子串
[LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列
All LeetCode Questions List 题目汇总
[LeetCode] 125. Valid Palindrome 验证回文字符串的更多相关文章
- 125 Valid Palindrome 验证回文字符串
给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...
- LeetCode 125. Valid Palindorme (验证回文字符串)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [Leetcode] valid palindrome 验证回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- leetcode 125 验证回文字符串 Valid Palindrome
验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...
- LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1
680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...
随机推荐
- 使用Arduino开发板连接干簧管(Reed Switch)的方法
在现实生活中,干簧管(Reed Switch)有许多重要的应用,如磁性门开关.笔记本电脑.智能手机等.在本篇文章中,我们将了解一些干簧管的知识,并介绍如何使用Arduino开发板连接干簧管. 干簧管( ...
- MSP430 LaunchPad开发板入门教程集合
MSP-EXP430G2开发板是德州仪器提供的开发工具,也称为LaunchPad,用于学习和练习如何使用其微控制器产品.该开发板属于MSP430 Value Line系列,我们可以对所有MSP430系 ...
- Python 进程共享数据(数据传输)实例
#coding:utf-8 ''' Created on 2017年11月22日 @author: li.liu ''' import multiprocessing from time import ...
- Alpha冲刺(9/10)——2019.5.2
所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(9/10)--2019.5.2 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万 ...
- Java内存区域与内存溢出异常(jdk 6,7,8)
运行时数据区域 Java虚拟机在执行Java程序的过程中会把它关联的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,有些区域则依赖用户 ...
- Best practices for a new Go developer
https://blog.rubylearning.com/best-practices-for-a-new-go-developer-8660384302fc This year I had the ...
- 基于Helm和Operator的K8S应用管理
https://blog.csdn.net/RancherLabs/article/details/79483013 大家好,今天我们分享的内容是基于Helm和Operator的K8S应用管理. 我们 ...
- 【比赛题解】CSP2019 简要题解
D1T1 code 签到题,大家都会. 可以从高位往低位确定,如果遇到 \(1\),则将排名取反一下. 注意要开 unsigned long long. #include <bits/stdc+ ...
- 决策树——C4.5
-- coding: utf-8 -- """ Created on Thu Aug 2 17:09:34 2018 决策树ID3,C4.5的实现 @author: we ...
- jeecg uedit 自定义图片上传路径
jeecg uedit 图片上传配置自定义物理路径,简单描述:我们知道 jeecg 中使用的 uedit 默认图片上传路径为 "当前项目\plug-in\ueditor\jsp\upload ...