面试经典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;
    }
};
相关推荐
森G8 分钟前
48、柱状图---------QChart
c++·qt
charliejohn10 分钟前
计算机考研 408 数据结构 排序算法
数据结构
QH_ShareHub14 分钟前
反正态分布算法
算法
Tanecious.14 分钟前
蓝桥杯备赛:Day8-小苯的异或和
c++·蓝桥杯
float_com18 分钟前
LeetCode 27. 移除元素
leetcode
王老师青少年编程21 分钟前
csp信奥赛c++中的递归和递推研究
c++·算法·递归·递推·csp·信奥赛
Bczheng133 分钟前
五.serialize.h中的CDataStream类
算法·哈希算法
小O的算法实验室34 分钟前
2025年SEVC,考虑组件共享的装配混合流水车间批量流调度的多策略自适应差分进化算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
汀、人工智能37 分钟前
[特殊字符] 第36课:柱状图最大矩形
数据结构·算法·数据库架构·图论·bfs·柱状图最大矩形
List<String> error_P44 分钟前
蓝桥杯最后冲刺(三)
算法