LeetCode Valid Palindrome

原创 2015年07月08日 08:57:36

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;
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode[String]: Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori...

Leetcode-125 Valid Palindrome

1. 题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters a...

Leetcode 125. Valid Palindrome 回文验证 解题报告

1 解题思想就一个简单的判断一个string里面的字符和数字部分,是否是回文的,不考虑符号和大小写等其实说白了,就首尾指针对比一下就好。。 我代码里面首尾不同的地方时过滤下非字符数字2 原题Given...

【leetcode】125. Valid Palindrome

一、题目描述 Given a string, determine if it is a palindrome, considering only alphanumeric characters a...

LeetCode 125. Valid Palindrome 对撞指针(双索引)

一、题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

LeetCode OJ:Valid Palindrome

Valid Palindrome  Total Accepted: 5116 Total Submissions: 23808My Submissions Given a string, d...

leetcode 125 Valid Palindrome

讲道理,连着做了两个palindrome的题目,挺崩溃的。上一个题目是9题,求整数是否是palindrome,但是没有说不算负数。。。所以第一次提交的时候,还对负数进行了取绝对值之后的判断。同样,在这...

LeetCode - Valid Palindrome (判断“回文”)

Valid Palindrome

关于LeetCode中Valid Palindrome一题的理解

题目如下: Given a string, determine if it is a palindrome, considering only alphanumeric characters an...

Leetcode 125. Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)