[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 ...
随机推荐
- 微信红包签名算法 C#代码实现
string stringA = "appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=100001 ...
- Oracle 数据类型映射C#
Oracle 数据类型映射 下表列出 Oracle 数据类型及其与 OracleDataReader 的映射. Oracle 数据类型 由 OracleDataReader.GetValue 返回的 ...
- 根据硬件配置后mapred-site.xml和yarn-site.xml
机器总共16G内存,sqoop导入数据时大约需要2G左右 mapred-site.xml <configuration> <property> <name>mapr ...
- IntelliJ IDEA设置字符编码为UTF-8
File->Settings->Editor->File Encodings IDE Encoding: UTF-8 Project Encoding: UTF-8
- Oracle 11gR2 Database UNDO表空间使用率居高不下-转载
客户的数据库是Oracle Database 11.2.0.3.0 for AIX 6.1 64bit的单机数据库.客户查询DBA_FREE_SPACE发现UNDO表空间的使用率高达98%以上.客户的 ...
- 交易Txt文件导出
private void writeFYFileToTxt(List list, HttpServletRequest request, String drxh, FileOutputStream f ...
- 06-模仿系统的UIImageView
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- OC基础笔记目录
OC基础(1) Objective-C简介 OC和C对比 第一个OC程序 面向对象思想 OC基础(2) 类与对象 类的设计 第一个OC类 对象方法的声明和实现 类方法的声明和实现 OC基础(3) 对象 ...
- python写入中文到文件乱码的问题
file = open(filename,'a',encoding='utf8')#指定写入编码为utf8,否则写入中文会乱码
- JavaScript数据结构,队列和栈
在JavaScript中为数组封装了大量的方法,比如:concat,pop,push,unshift,shift,forEach等,下面我将使用JavaScript提供的这些方法,实现队列和栈的操作. ...