125 Valid Palindrome 验证回文字符串
给定一个字符串,确定它是否是回文,只考虑字母数字字符和忽略大小写。
例如:
"A man, a plan, a canal: Panama" 是回文字符串。
"race a car" 不是回文字符串。
注意:
你有考虑过这个字符串可能是空的吗? 在面试中这是一个很好的问题。
针对此题目,我们将空字符串定义为有效的回文字符串。
详见:https://leetcode.com/problems/valid-palindrome/description/
Java实现:
class Solution {
public boolean isPalindrome(String s) {
int size=s.length();
if(s.isEmpty()||size==0){
return true;
}
char[] chars=s.toLowerCase().toCharArray();
int left=0;
int right=size-1;
while(left<right){
if(!isAlphaOrNum(chars[left])){
++left;
}else if(!isAlphaOrNum(chars[right])){
--right;
}else if(chars[left]==chars[right]){
++left;
--right;
}else{
return false;
}
}
return true;
}
private boolean isAlphaOrNum(char ch){
if(ch>='a'&&ch<='z'){
return true;
}
if(ch>='0'&&ch<='9'){
return true;
}
return false;
}
}
125 Valid Palindrome 验证回文字符串的更多相关文章
- [LeetCode] 125. 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 ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 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 ...
- [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 ...
随机推荐
- java8--面向对象 下(疯狂java讲义3) 复习笔记
1.如果一个包装类和一个基本类型比较大小,或者是两个基本类型进行比较大小,直接用==就好: 如果是两个包装类进行比较大小,那么使用equals(),返回值是true,false,或者使用Xxx.com ...
- Writing a Simple YARN Application 从hadoop生态抽出yarn ,单独使用yarn
Apache Hadoop 2.9.1 – Hadoop: Writing YARN Applications https://hadoop.apache.org/docs/current/hadoo ...
- 利用WebViewJavascriptBridge与UIWebView进行交互
事情的起因还是因为项目需求驱动.折腾了两天,由于之前没有UIWebView与JS交互的经历,并且觉得这次在功能上有一定的创造性,特此留下一点文字,方便日后回顾. 我要实现这样一个需求:按照本地的CSS ...
- mysql优化-----索引覆盖
一道面试题: 有商品表, 有主键,goods_id, 栏目列 cat_id, 价格price 说:在价格列上已经加了索引,但按价格查询还是很慢,问可能是什么原因,怎么解决? 答:在实际场景中,一个电商 ...
- JS中的存储机制
一.堆和栈的介绍 1.堆和队,是先进先出:栈,是先进后出,就跟水桶差不多: 2.存储速度:堆和队的存储速度较慢,栈的存储速度较快,会自动释放: 二.js中存储的类型 1.堆,一般用于复杂数据类型,存储 ...
- HDU1300 Pearls —— 斜率优化DP
题目链接:https://vjudge.net/problem/HDU-1300 Pearls Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- unity-Fatal Error GC-GetThreadContext Failed
这几次在使用unity5.3打windows包后,运行x.exe不久总是会弹出"fatal error GC: GetThreadContext Failed"的错误.到网上查了, ...
- 书写优雅的shell脚本(插曲)- /proc/${pid}/status
Linux中/proc/[pid]/status详细说明 博客分类: OS Linux多线程 [root@localhost ~]# cat /proc/self/status Name: cat ...
- 书写优雅的shell脚本(一)- if语句
使用unix/linux的程序人员几乎都写过shell脚本,但这其中很多人都是为了完成功能而在网上找代码段,这样写出来的shell脚本在功能方面当然是没有什么问题,但是这样的方式不能写出优雅的shel ...
- Linux 命令行命令及参数辨异
1. 软链接与硬链接 ln -s 源文件 目标文件 当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目 ...