[leetcode]_Valid Palindrome
题目:判断一个数字字符串是否是回文串。空串认为是回文串。
思路:双指针问题,重点在于此题的很多陷进:例如,s = " " ,return true。 s = ".," , return true。
代码:修改了很多遍,终于AC , 要点在于只有当头尾两个指针都指向数字或者字母时,此时才有比较操作,否则都认为是相等的。
public boolean isPalindrome(String s) {
int i = 0 , j = s.length() - 1;
char left = 0 , right = 0 ;
while( i <= j ){ // 控制循环结束,所有元素都已经遍历过了。
left = s.charAt(i);
if(!ifLegal(left)){
i++;
continue;
}
right = s.charAt(j);
if(!ifLegal(right)) {
j--;
continue;
}
//只有当left 和 right 同时指向数字、字母时,才进行比较;其它情况都认为是相等的。
if((left != right && Math.abs(left - right) != ('a' - 'A'))) return false;
i++;
j--;
}
return true;
}
public boolean ifLegal(char ch){
if(ch >= 'a' && ch <= 'z') return true;
if(ch >= 'A' && ch <= 'Z') return true;
if(ch >= '0' && ch <= '9') return true;
return false;
}
[leetcode]_Valid Palindrome的更多相关文章
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode Longest Palindrome
原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...
- LeetCode Shortest Palindrome
原题链接在这里:https://leetcode.com/problems/shortest-palindrome/ 题目: Given a string S, you are allowed to ...
- leetcode@ [336] Palindrome Pairs (HashMap)
https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- 判断DataTable是否为空
DataTable: ) { //为空的操作 } DataRow: if(!DataRow.IsNull("列名")) { //不为空的操作 }
- 防止重复发送 Ajax 请求
作者:长天之云链接:http://www.zhihu.com/question/19805411/answer/15465427来源:知乎 不推荐用外部变量锁定或修改按钮状态的方式,因为那样比较难: ...
- java利用过滤器实现编码的转换,内容输出的替换
在页面建个表单 <form action="login.do" method="post"> <input type="text&q ...
- windows平台快速安装 matplotlib
版本:Python2.7.9 Pip 1.5.6 如果没有安装VC2008以上版本,就需要工具Microsoft Visual C++ Compiler for Python 2.7 http://w ...
- 光流算法:Brox光流的OpenCV源码解析
OpenCV中DeepFlow代码其实是Brox光流,而非真正的DeepFlow光流,在将近一个月的研究.移植及优化过程中,对Brox光流有了较深刻的认识.我对OpenCV中源码进行了详细的分析,并以 ...
- Ext中 get、getDom、getCmp的区别
getDom方法能够得到文档中的DOM节点,该方法中包含一个参数,该参数可以是DOM节点的id.DOM节点对象或DOM节点对应的Ext元素(Element)等. (与getElementById是一个 ...
- [POJ 1385] Lifting the Stone (计算几何)
题目链接:http://poj.org/problem?id=1385 题目大意:给你一个多边形的点,求重心. 首先,三角形的重心: ( (x1+x2+x3)/3 , (y1+y2+y3)/3 ) 然 ...
- & replace &
var decoded = encoded.replace(/&/g,'&'); http://stackoverflow.com/questions/3700326/decode-a ...
- jsp页面
//获取根目录 ${pageContext.request.contextPath} //(父页面提交,嵌入页面显示)提交表单,设置响应方法和返回结果页面显示在frame中 <input typ ...
- 如何隐藏storyboard中的top bar
在navigation bar中是没有设置它的隐藏和显示的属性的 那么究竟在什么地方呢,当在它自身的属性中没有找到的情况下,那么就只能去包含它的容器中去寻找了,结果果然找到了.在view的第四个选择器 ...