【LeetCode】- 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.

[ 解法1: ]

先把有效的字母、数字准备好,然后遍历目标字符串,有效的字符放入buffer。

再比较buffer中的字符串和反转后的字符串,如果相同那就是回文,否则不是回文。

点评:这个解法可以被系统accept,但调用了太多api,性能一般。

public class Solution {

	public boolean isPalindrome(String s) {
		StringBuilder buffer = new StringBuilder();
		String tempStr = "abcdefghijklmnopqrstuvwxyz0123456789";
		char[] cArr = s.toCharArray();
		for (int i = 0; i < cArr.length; i++) {
			if (tempStr.contains(String.valueOf(cArr[i]).toLowerCase())) {
				buffer.append(String.valueOf(cArr[i]).toLowerCase());
			}
		}
		String currentStr = buffer.toString();
		String reverseStr = buffer.reverse().toString();
		if (currentStr.equals(reverseStr)) {
			return true;
		}
		return false;
	}

}

时间: 2024-08-09 22:00:23

【LeetCode】- Valid Palindrome(正确的回文)的相关文章

leetcode题解: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 tha

【LeetCode每天一题】 Valid Palindrome(有效的回文)

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a canal: Panama" O

[leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. 思

[LeetCode] Longest Palindrome 最长回文串

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the leng

LeetCode 9 Palindrome Number (回文数)

翻译 确定一个整数是否是回文数.不能使用额外的空间. 一些提示: 负数能不能是回文数呢?(比如,-1) 如果你想将整数转换成字符串,但要注意限制使用额外的空间. 你也可以考虑翻转一个整数. 然而,如果你已经解决了问题"翻转整数(译者注:LeetCode 第七题), 那么你应该知道翻转的整数可能会造成溢出. 你将如何处理这种情况? 这是一个解决该问题更通用的方法. 原文 Determine whether an integer is a palindrome. Do this without ex

[C++]LeetCode: 121 Palindrome Partitioning (分割回文子串 回溯法)

题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",&

[LeetCode] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

LeetCode 9 Palindrome Number(回文数字判断)

Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的位数.按照已知数字x对10进行多次求余运算,可以得到数字位数. 具体思路: 1.每次取出该数字的最高位和最低位进行比较. 2.如果不相等则直接返回FALSE, 3.如果相等修改x的值(去掉最高位也同时去掉最低位)其中去掉最高位可以通过求模运算,去掉最低位可以采用除以10 4.进行循环直到x的值不大于0为止

Leetcode 9. Palindrome Number(判断回文数字)

Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You

LeetCode OJ Palindrome Number(回文数)

1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 int r=0,init=x; 5 if(init==0) return true; 6 if(init<0) return false; 7 while(init!=0){ 8 r=r*10+init%10; 9 init=init/10; 10 } 11 if(r==x) 12 return true; 13 else 14 return false; 15 } 16 };