LeetCode Valid Palindrome

Description:

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.

Solution:

将所有字母和数字的char取出,然后判断即可。

import java.util.*;

public class Solution {
	public boolean isPalindrome(String s) {
		int len = s.length();
		char ch[] = new char[len];
		s = s.toLowerCase();

		int tot = 0;
		for (int i = 0; i < len; i++) {
			char c = s.charAt(i);
			if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
				ch[tot++] = c;
		}

		for (int i = 0; i < tot; i++)
			if (ch[i] != ch[tot - 1 - i])
				return false;

		return true;
	}
}


阅读更多
文章标签: java leetcode
个人分类: 算法/oj
上一篇LeetCode Best Time to Buy and Sell Stock III
下一篇LeetCode Repeated DNA Sequences
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭