面试经典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;
    }
};
相关推荐
龙砚序2 分钟前
【数据结构|二叉树实战 05-1】文件系统目录树构建与统计模块(C 语言递归实现)
数据结构
极客先躯24 分钟前
智能体开发高级面试每日一题|2026-06-01|Agent 系统中 LLM 扮演什么角色?除了 LLM 还需要哪些非 LLM 组件?
ai·面试·llm·agent·每日一题·高级面试题·智能体开发
fpcc1 小时前
面试系列—虚函数常见的面试问题
c++·面试
Shaoshing1 小时前
LeetCode100 76.最小覆盖字串
java·算法·leetcode
不知棋者1 小时前
蓝桥杯 2020 省赛 B 组既约分数:从暴力枚举到欧拉函数前缀和
c++·算法·职场和发展·蓝桥杯
电科_银尘1 小时前
【大模型算法与工程 63】视觉语言模型:视觉编码器如何接入LLM
人工智能·算法·语言模型
小蒋学算法1 小时前
算法-三个无重叠子数组的最大和-前缀和+前缀和最值维护
数据结构·算法
新知图书2 小时前
推荐一本 C++ 视觉开发书:《OpenCV 4.5 计算机视觉开发实战(基于 VC++)》,89 元,几十所高校在用
c++·opencv·计算机视觉
揽星河..2 小时前
【C++】模板进阶
c++·算法
凌冰_2 小时前
数据结构 - 矩阵(九)
算法·矩阵