题目链接:https://leetcode.com/problems/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.
算法:
[java] view
plain copy
- public boolean isDecent(char c) {
- if (Character.isAlphabetic(c) || Character.isDigit(c)) {
- return true;
- } else
- return false;
- }
- public boolean isPalindrome(String s) {
- char c[] = s.toLowerCase().toCharArray();
- int i = 0, j = c.length - 1;
- while (i < j) {
- if (isDecent(c[i]) && isDecent(c[j])) {
- if (c[i] == c[j]) {
- i++;
- j--;
- } else {
- return false;
- }
- }
- if (!isDecent(c[i])) {
- i++;
- }
- if (!isDecent(c[j])) {
- j--;
- }
- }
- return true;
- }
时间: 2024-10-11 09:34:54