LeetCode 125. Valid Palindorme (验证回文字符串)
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 (验证回文字符串)的更多相关文章
- [LeetCode] 125. Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 125 Valid Palindrome 验证回文字符串
给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写.例如:"A man, a plan, a canal: Panama" 是回文字符串."race a c ...
- [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] 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 ...
- LeetCode(125):验证回文串
Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
随机推荐
- android studio java.io.IOException:setDataSourse fail.
这一次是针对Android开发中的一个小问题,权限获取的问题. 在写了一个一个小Android程序的时候,有时候普需要获取本机的文件(Audio&Video),这时候如果不加权限就会出现这种情 ...
- 扩增子分析QIIME2-3数据导出Exporting data
# 激活工作环境 source activate qiime2-2017.8 # 建立工作目录 mkdir -p qiime2-exporting-tutorial cd qiime2-exporti ...
- CPU总线
总线(Bus)是计算机各种功能部件之间传送信息的公共通信干线,它是由导线组成的传输线束.按照计算机所传输的信息种类,计算机的总线可以划分为数据总线.地址总线和控制总线,分别用来传输数据.数据地址和控制 ...
- vue组件---组件注册
(1)组件名 在注册一个组件的时候,我们始终需要给它一个名字.比如在全局注册的时候我们已经看到了: Vue.component('my-component-name', { /* ... */ }) ...
- 17Web应用乱码问题
Web应用乱码问题 Web应用乱码问题 简介 每个国家(或区域)都规定了本国家(或地区)计算机信息交换用的字符编码集,如美国的扩展ASCII码, 中国的GB2312-80,日本的JIS 等,作为该国家 ...
- ORB-SLAM2:一种开源的VSLAM方案(译文)
摘要: ORB-SLAM2是基于单目,双目和RGB-D相机的一套完整的SLAM方案.它能够实现地图重用,回环检测和重新定位的功能.无论是在室内的小型手持设备,还是到工厂环境的无人机和城市里驾驶的汽车, ...
- Codeforces Round #470 Div. 2题解
A. Protect Sheep time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 「 Luogu P1231 」 教辅的组成
题目大意 有 $\text{N1}$ 本书 $\text{N2}$本练习册 $\text{N3}$本答案,一本书只能和一本练习册和一本答案配对.给你一些书和练习册,书和答案的可能的配对关系.问你最多可 ...
- UVA - 11212 Editing a Book (IDA*搜索)
题目: 给出n(1<n<10)个数字组成的序列,每次操作可以选取一段连续的区间将这个区间之中的数字放到其他任意位置.问最少经过多少次操作可将序列变为1,2,3……n. 思路: 利用IDA* ...
- linux常用操作记录
vim:多行注释 vim中多行注释和多行删除命令,这些命令也是经常用到的一些小技巧,可以大大提高工作效率. 多行注释: 1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区 ...