面试经典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;
    }
};
相关推荐
代码游侠几秒前
应用——Linux Framebuffer 图形库显示
linux·运维·服务器·数据库·笔记·算法
胖咕噜的稞达鸭3 分钟前
算法日记分治:用归并排序解决逆序对问题
算法
我叫袁小陌12 分钟前
C++内存分布详解
开发语言·c++
代码or搬砖15 分钟前
JVM垃圾回收算法
jvm·算法
Aaron158815 分钟前
基于RFSOC+VU13P在5G波束成形中的技术应用分析报告
人工智能·算法·5g·fpga开发·硬件架构·信息与通信·基带工程
Hi2024021715 分钟前
如何向Virtual Audio Cable写入自定义音频数据
c++·windows·音视频·virtualaudio·虚拟音频线
小丁努力不焦虑16 分钟前
常考算法题
算法
C雨后彩虹18 分钟前
亲子游戏问题
java·数据结构·算法·华为·面试
ht巷子22 分钟前
Qt:QPainter坐标系统和坐标转换
开发语言·c++·qt
HalvmånEver22 分钟前
Linux:基于匿名管道创建出简易进程池(进程间通信五)
linux·运维·服务器·c++·进程池·管道pipe