面试经典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 分钟前
Linux内核与驱动:8.ioctl驱动基础
linux·c++
Allen_LVyingbo11 分钟前
量子测量三部曲:投影测量、POVM 与坍缩之谜—从形式主义到物理图像
算法·性能优化·健康医疗·量子计算·空间计算
云栖梦泽13 分钟前
Linux内核与驱动:7.从应用层 lseek() 到驱动层 .llseek,Linux 字符设备偏移控制详解
linux·c++
qiqsevenqiqiqiqi16 分钟前
位运算 计算
算法
steins_甲乙21 分钟前
从0做一个小型内存泄露检测器(2): elf文件的动态链接
c++
甄心爱学习27 分钟前
【最优化】1-6章习题
人工智能·算法
charlie11451419127 分钟前
通用GUI编程技术——图形渲染实战(二十八)——图像格式与编解码:PNG/JPEG全掌握
开发语言·c++·windows·学习·图形渲染·win32
PD我是你的真爱粉27 分钟前
向量数据库原理与检索算法入门:ANN、HNSW、LSH、PQ 与相似度计算
数据库·人工智能·算法
汀、人工智能30 分钟前
[特殊字符] 第72课:杨辉三角
数据结构·算法·数据库架构·图论·bfs·杨辉三角
Ricky_Theseus39 分钟前
C++静态库
开发语言·c++