面试经典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;
    }
};
相关推荐
YaraMemo23 分钟前
数学优化问题中的三大转化:多目标转化为单目标,多变量转化为单变量,有约束转化为无约束
人工智能·算法·5g·信息与通信·信号处理
CSCN新手听安24 分钟前
【Qt】Qt窗口(六)QMessageBox消息对话框的使用
开发语言·c++·qt
Ailan_Anjuxi27 分钟前
【附Python源码】使用minGPT训练自己的小型GPT语言模型
算法
会编程的土豆30 分钟前
由c/c++速通go语言,新手必看
c语言·c++·golang
QuZero35 分钟前
StampedLock Mechanism
java·算法
云泽80839 分钟前
二叉树高阶笔试算法题精讲(二):非递归遍历与序列构造全解析
c++·算法·面试
Cosolar1 小时前
大型语言模型(LLM)微调与量化技术全指南——从预训练到高效部署
人工智能·后端·面试
爱看书的小沐1 小时前
【小沐学WebGIS】基于Cesium.JS与jsbsim联动三维飞行仿真(OpenGL、Cesium.js、Three.js)
c++·qt·three.js·opengl·cesium·jsbsim
zh_xuan1 小时前
api测试工具支持代理
c++·libcurl
夜猫逐梦1 小时前
[开发经验] DLL注入中控制台窗口无法关闭的排查与修复
c++·windows·控制台