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.

去掉S中的无关字符,将大写字母转换为小写,之后就是简单的回文串判断。

自己写的C语言版的,可以实现效果,但是会提示超时

bool isPalindrome(char *s) {
    int j = 0;
    for(int i=0; i<strlen(s); i++){
        if(s[i]>=‘0‘ && s[i]<=‘9‘ || s[i]>=‘a‘ && s[i]<=‘z‘ || s[i]>=‘A‘ && s[i]<=‘Z‘){
            if(s[i]>=‘A‘ && s[i]<=‘Z‘){
                s[j++] = s[i]-‘A‘+‘a‘;
            }else{
                s[j++] = s[i];
            }
        }
    }
    s[j] = ‘\0‘;
    if(j <= 1) return true;
    for(int i=0; i<strlen(s); i++){
        if(s[i] != s[strlen(s)-i-1]){
            return false;
        }
    }
    return true;
}

网上找的C++版的,验证可以使用

class Solution {
public:
    bool isPalindrome(string s) {
        string t = "";
        for (int i = 0; i < s.length(); i++) {
            if (s[i] >= ‘a‘ && s[i] <= ‘z‘ || s[i] >= ‘0‘ && s[i] <= ‘9‘ || s[i] >= ‘A‘ && s[i] <= ‘Z‘) {
                if (s[i] >= ‘A‘ && s[i] <= ‘Z‘) {
                    t += (s[i] - ‘A‘ + ‘a‘);
                }
                else {
                    t += s[i];
                }
            }
        }
        if (t == "") {
            return true;
        }
        for (int i = 0; i < t.length() / 2; i++) {
            if (t[i] != t[t.length() - i - 1]) {
                return false;
            }
        }
        return true;
    }
};
时间: 2024-10-18 05:16:44

LeetCode——Valid Palindrome (回文判断)的相关文章

leetcode4 Valid Palindrome回文数

Valid Palindrome回文数 [email protected] Question: 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" i

LeetCode: Palindrome 回文相关题目

LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioning II 解题报告 Leetcode:[DP]Longest Palindromic Substring 解题报告 LeetCode: Valid Palindrome 解题报告

LeetCode: Valid Palindrome [125]

[题目] 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

[LeetCode] Valid Palindrome [10]

题目 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 t

2_3 回文判断

#include <stdio.h> #include <string.h> void main() { int x,i; char str[100]; //gets(st1); printf("Please input a string to find out whether the string is palindrome or not\n"); scanf("%s",str); x=strlen(str); for(i = 0; i &

DS之顺序栈和链队实现回文判断

顺序栈和链队的基本操作就不再一一列举了,要想实现回文判断,先来了解什么是回文?"回文"一字符串正着读和反着读是相同的字符序列,如"abcba","abba"为"回文","abab"则不是"回文". 其次就是顺序栈和链队如何实现回文的判断?将输入的字符串依次入栈和入队,然后再依次出栈和出队,由于入栈和入队是相同的序列,然而出栈和出队是相反的序列,这就实现了回文的判断. 最后考虑要用到顺序栈

冒泡排序与回文判断

冒泡排序:很简单就不细说了: #include <stdio.h> void bubbleSort(int num[],int len) { int i = 0; int j = 0; int temp = 0; for(j = 0;j<len-1;j++) { for(i = 0;i < len - j-1;i++) { if(num[i] > num[i+1]) { temp = num[i+1]; num[i+1] = num[i]; num[i] = temp; }

前端与算法 leetcode 125. 验证回文串

目录 # 前端与算法 leetcode 125. 验证回文串 题目描述 概要 提示 解析 解法一:api侠 解法二:双指针 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端与算法 leetcode 125. 验证回文串 题目描述 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: tru

LeetCode Valid Palindrome 有效回文(字符串)

1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if(s=="") return true; 5 if(s.length()==1) return true; //单个字符,对称 6 char *p,*q; 7 p=&s[0]; //p指向开头 8 q=&s[s.length()-1]; //q指向末尾 9 while(p!=q){ 10 //测试字符串里是否有字母或数字,若没有,则立刻返回