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.


题目标签:String, Two Pointers

  题目给了我们一个string s, 让我们判断它是不是 palindorme。

  对于s, 我们只需要比较 字母 和 数字。

设置一个 left = 0, right = s.length() -1,依次比较,其中遇到任何其他 char 就跳过,具体看code。

  一开始不知道有 Character.isLetterOrDigit() ,还分别用了 letter 和 digit - -

Java Solution:

Runtime beats 80.01%

完成日期:03/07/2017

关键词:two pointers

关键点:skip all non-alphanumeric chars

 class Solution
{
public boolean isPalindrome(String s)
{
// use two pointers
int left = 0;
int right = s.length() - 1; char [] char_arr = s.toCharArray(); while(left < right)
{
while(left < right && !Character.isLetterOrDigit(char_arr[left])) // if char is not letter or digit
left++; while(left < right && !Character.isLetterOrDigit(char_arr[right])) // if char is not letter or digit
right--; if(Character.toUpperCase(char_arr[left]) != Character.toUpperCase(char_arr[right]))
return false; left++;
right--;
} return true;
} }

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 125. Valid Palindorme (验证回文字符串)的更多相关文章

  1. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. 125 Valid Palindrome 验证回文字符串

    给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...

  3. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LintCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. [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 ...

  7. leetcode 125 验证回文字符串 Valid Palindrome

    验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...

  8. LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

  9. LeetCode(125):验证回文串

    Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...

随机推荐

  1. Jmeter接口测试---JDBC简单实践

    我的环境:MySQL:mysql-5.6.24-win32 jdbc驱动:mysql-connector-java-5.1.22-bin.jar JMeter:apache-jmeter-2.13 1 ...

  2. IT项目为什么失败 --美国IT项目管理硕士笔记(一)

    IT项目为什么失败 什么是项目   项目可以被看作任何一系列的活动和任务.这些活动和任务有一个特定目标需要在特定要求下完成,并有一个明确的开始结束日期和资金限制(如果有).项目需要消耗人力或非人力资源 ...

  3. Exception Information

    https://developer.apple.com/library/content/technotes/tn2004/tn2123.html Exception Information The t ...

  4. Intel Processor Exception Handling

    当一个进程的行为超出预期时,系统将把它kill掉. On Intel IA-32 and Intel 64 architecture processors, each architecturally- ...

  5. cgroup代码浅析(1)

    前置:这里使用的linux版本是4.8,x86体系. cgroup_init_early(); 聊这个函数就需要先了解cgroup. cgroup概念 这个函数就是初始化cgroup所需要的参数的.c ...

  6. Python之实例属性和类属性

    参考原文 廖雪峰Python 实例属性和类属性 在前面已经说过由于Python是动态语言,可以根据类的实例绑定任何的属性. 给实例绑定属性的方法是通过实例变量,或者self变量绑定的: class S ...

  7. 【Python实践-9】将字符串转化为浮点型

    利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456. 思路:计算小数位数--->将字符串中的小数点去掉--->字符串转换为整数-- ...

  8. 洛谷 2344 奶牛抗议 Generic Cow Protests, 2011 Feb

    [题解] 我们可以轻松想到朴素的状态转移方程,但直接这样做是n^2的.所以我们考虑采用树状数组优化.写法跟求逆序对很相似,即对前缀和离散化之后开一个权值树状数组,每次f[i]+=query(sum[i ...

  9. [bzoj1005][HNOI2008][明明的烦恼] (高精度+prufer定理)

    Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为N ...

  10. Intellij IDEA神器居然还有这些小技巧---超级好用的

    Intellij IDEA神器居然还有这些小技巧----https://my.oschina.net/samgege/blog/1808622?p=8