面试经典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;
    }
};
相关推荐
wayz119 分钟前
Day 5:KNN算法与相似K线匹配
人工智能·算法·机器学习
晨曦中的暮雨19 分钟前
Java集合类型主要有哪些?以及各自原理
数据结构·算法
lixinnnn.31 分钟前
01BFS:Three States
算法
晓纪同学1 小时前
EffctiveC++_第三章_资源管理
开发语言·c++·算法
沐雪轻挽萤1 小时前
6. C++17新特性-编译期 if 语句 (if constexpr)
开发语言·c++
水云桐程序员1 小时前
C语言编程基础,输入与输出
c语言·开发语言·算法
ZPC82101 小时前
MoveIt Servo 与自己编写的 Action Server 通信
人工智能·算法·机器人
jllllyuz1 小时前
采用核函数的极限学习机(KELM)MATLAB实现
算法
apcipot_rain1 小时前
【天梯赛】2026天梯赛模拟赛——题解
开发语言·c++·算法·蓝桥杯·天梯赛
-To be number.wan1 小时前
重新认识一下“私有继承”
c++·学习