面试经典159题——Day25

文章目录

一、题目

125. Valid Palindrome

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: s = "A man, a plan, a canal: Panama"

Output: true

Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"

Output: false

Explanation: "raceacar" is not a palindrome.

Example 3:

Input: s = " "

Output: true

Explanation: s is an empty string "" after removing non-alphanumeric characters.

Since an empty string reads the same forward and backward, it is a palindrome.

Constraints:

1 <= s.length <= 2 * 105

s consists only of printable ASCII characters.

题目来源: leetcode

二、题解

cpp 复制代码
class Solution {
public:
    bool isPalindrome(string s) {
        int n = s.length();
        string tmp = "";
        for(int i = 0;i < n;i++){
            if(isdigit(s[i]) || isalpha(s[i])) tmp += s[i];
        }
        int i = 0,j = tmp.length() - 1;
        while(i < j){
            if(tolower(tmp[i]) != tolower(tmp[j])) return false;
            i++;
            j--;
        }
        return true;
    }
};
相关推荐
夜月yeyue18 小时前
个人写HTOS移植shell
c++·mcu·算法·性能优化·架构·mfc
ajassi200018 小时前
开源 C++ QT QML 开发(九)文件--文本和二进制
c++·qt·开源
Nix Lockhart18 小时前
《算法与数据结构》第七章[算法3]:图的最小生成树
c语言·数据结构·算法
UrbanJazzerati18 小时前
一文看懂指数函数:基础与性质
面试
-森屿安年-18 小时前
C++ 类与对象
开发语言·c++
十重幻想19 小时前
PTA6-5 使用函数求1到10的阶乘和(C)
java·c语言·算法
ajassi200019 小时前
开源 C++ QT QML 开发(八)自定义控件--圆环
c++·qt·开源
名誉寒冰19 小时前
【LeetCode】454. 四数相加 II 【分组+哈希表】详解
算法·leetcode·散列表
深思慎考19 小时前
Linux二进制查看工具——hexdump
linux·c++·二进制·文件查看·hexdump
crystal_pin19 小时前
前端多端适配与Electron思路
面试