Valid Palindrome
来源:互联网 发布:网络作家稿费 编辑:程序博客网 时间:2024/04/26 00:29
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.
思路:左右指针移动,首先转换成lowercase,然后判断是不是alphanumeric,如果不是,左移右移直到left>=right.
if(left>right) break; 这句话一定要加上,别忘了。
public class Solution { public boolean isPalindrome(String s) { if(s == null) return false; int left = 0; int right = s.length()-1; while(left<=right){ while(left<s.length() && !isvalid(s.charAt(left))){ left++; } while(right>=0 && !isvalid(s.charAt(right))){ right--; } if(left>=right) break; if(changeToLowerCase(s.charAt(left)) != changeToLowerCase(s.charAt(right))){ return false; } left++; right--; } return true; } public boolean isvalid(char c){ return ('0'<=c && c<='9') ||('a'<=c && c<='z') ||('A'<=c && c<='Z'); } public char changeToLowerCase(char c){ if('a'<=c && c<='z') return c; int diff = 'a'-'A'; return (char)(c+diff); } }
0 0
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- valid palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Qt Testlib 测试GUI(1)
- shoelace源代码分析之shoe.c
- 分割成回文需要的最小分割数 Palindrome Partitioning II @LeetCode
- Android文件下载使用Http协议
- Qt Testlib 测试GUI(2)
- Valid Palindrome
- OOP Design (Head First Design Patterns) 学习笔记--03 Observer Pattern
- [LeetCode] Longest Substring Without Repeating Characters
- Oracle报错:ORA-01033:ORACLE initialization or shutdown in process
- 结合示例说明类模块与多态
- 学习也是一种兴趣爱好
- 2014年祝福有情,安康!幸福!吉祥!马到成功!
- 为考研弃考者痛
- 元旦快乐