leetcode 125. Valid Palindrome ----- java
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、两个栈,从前向后和从后向前,然后判断两个栈是否相同。
public class Solution {
public boolean isPalindrome(String s) {
Stack stack1 = new Stack<Character>();
Stack stack2 = new Stack<Character>();
s = s.toLowerCase();
char[] word = s.toCharArray();
int len = s.length();
for( int i = 0;i<len;i++){
if( (word[i] >= '0' && word[i] <= '9') || (word[i]>='a' && word[i]<='z') )
stack1.push(word[i]);
if( (word[len-1-i] >= '0' && word[len-1-i] <= '9') || (word[len-1-i]>='a' && word[len-1-i]<='z') )
stack2.push(word[len-1-i]);
} return stack1.equals(stack2); }
}
2、直接用两个指针记录left和right即可。
public class Solution {
public boolean isPalindrome(String s) { char[] word = s.toLowerCase().toCharArray();
int len = s.length();
int left = 0,right = len-1;
while( left < right ){ if( !((word[left] >= '0' && word[left] <= '9') || (word[left]>='a' && word[left]<='z' )) )
left++;
else if( !((word[right] >= '0' && word[right] <= '9') || (word[right]>='a' && word[right]<='z' )) )
right--;
else if( word[left] == word[right]){
left++;
right--;
}else
return false;
}
return true; }
}
leetcode 125. Valid Palindrome ----- java的更多相关文章
- Java [Leetcode 125]Valid Palindrome
题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- [LeetCode] 125. 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 a ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...
- Java for LeetCode 125 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Leetcode 125 Valid Palindrome 字符串处理
题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...
- [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 ...
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
随机推荐
- Android 获取网络状态
1.检测网络是否可用 public boolean isNetWorkConnected() { ConnectivityManager cm = (ConnectivityManager)getSy ...
- 学习MVC的一些随笔简单记录
1 视图本身没有它所要显示的数据,视图的数据源始终是控制器 3 游戏的进行是模型的一部分,不是控制器的一部分 4 模型关于游戏是什么,在模型中封装游戏进行的逻辑,模型对用户界面一无所知,里面没有任何同 ...
- Java基础毕向东day04
1. 数组 2.选择排序.冒泡排序.折半查找.
- Android IPC(inter-process Communitcation)
Android IPC(inter-process Communitcation) http://www.cnblogs.com/imlucky/archive/2013/08/08/3246013. ...
- linux基础命令学习(六)DHCP服务器配置
工作原理: 1.客户机寻找服务器:广播发送discover包,寻找dhcp服务器 2.服务器响应请求:单播发送offer包,对客户机做出响应.提供客户端网络相关的租约以供选 ...
- osmocom-bb中用osmocon刷入固件命令那些参数你都弄懂了吗?
转载留做备份,原文地址:http://92ez.com/?action=show&id=23341 首先找到osmocon.c这个源文件,具体目录在这里 osmocom-bb/src/host ...
- BZOJ 1486 最小圈
二分答案是显然的,我们需要dfs版spfa判一下负环. 看起来是n^2其实很快. #include<iostream> #include<cstdio> #include< ...
- 爬虫再探之mysql简单使用
在爬取数据量比较大时,用EXCEL存取就不太方便了,这里简单介绍一下python操作mysql数据库的一些操作.本人也是借助别人的博客学习的这些,但是找不到原来博客链接了,就把自己的笔记写在这里,这里 ...
- 看了这篇文章,Java编程速度我都惊呆了
熟记于心,打遍天下,(开始装了) 保存 Ctrl+s (这个就不用解释了吧!!!!) 注释代码 Ctrl+/ 取消注释 Ctrl+/代码辅助 Alt+/ 快速修复 ...
- “PEDIY CrackMe 2007” 下载地址
工欲善其事,必先利其器.本专辑收集了看雪论坛『CrackMe & ReverseMe』版块2004年4月-2006年12月31期间所有的CrackMe和ReverseMe,共350余个. 下载 ...