leetcode Valid Palindrome 难度系数2 2.25
2014-01-28 11:51
176 查看
Question:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
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.
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.
public class Solution { public boolean isPalindrome(String s) { if (s == null || s.length() == 0) { return true; } String sUsed = s.toLowerCase().trim(); int left = 0; int right = sUsed.length() - 1; while (left < right) { if (!(sUsed.charAt(left) <= 'z' && sUsed.charAt(left) >= 'a') && !(sUsed.charAt(left) <= '9' && sUsed.charAt(left) >= '0')) { ++left; } else if (!(sUsed.charAt(right) <= 'z' && sUsed.charAt(right) >= 'a') && !(sUsed.charAt(right) <= '9' && sUsed.charAt(right) >= '0')) { --right; } else if (sUsed.charAt(right) != sUsed.charAt(left)) { return false; } else if (sUsed.charAt(right) == sUsed.charAt(left)) { ++left; --right; } } return true; } }
相关文章推荐
- leetcode Pascal's Triangle 2.22 难度系数2
- leetcode 3Sum 难度系数 3.6
- leetcode JAVA Set Matrix Zeroes 难度系数3 3.24
- leetcode Pascal's Triangle II 2.23 难度系数2
- leetcode 3Sum Closest 难度系数3 3.7
- leetcode JAVA Search a 2D Matrix 难度系数3 3.25
- leetcode JAVA Reverse Words in a String 难度系数2-3?最新题151
- leetcode Symmetric Tree 1.8 难度系数1
- leetcode Valid Sudoku 2.12 难度系数2
- leetcode 4Sum 难度系数3 3.9
- leetcode Generate Parentheses 难度系数3 3.10
- leetcode JAVA Combinations 难度系数3 3.26
- leetcode JAVA Partition List 难度系数3 3.30
- leetcode Merge k Sorted Lists 难度系数3 3.11
- leetcode JAVA Subsets 难度系数3 3.27
- leetcode JAVA Substring with Concatenation of All Words 难度系数3 3.12
- leetcode JAVA Word Search 难度系数3 3.28
- leetcode JAVA Decode Ways 难度系数3 3.31
- leetcode Balanced Binary Tree 1.10 难度系数1
- leetcode JAVA Combination Sum 难度系数3 3.13