面试经典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;
    }
};
相关推荐
前端小巷子1 分钟前
浏览器的渲染流程:从 HTML 到屏幕显示
前端·javascript·面试
寒山李白4 分钟前
Java中高并发线程池的相关面试题详解
java·开发语言·面试·高并发·线程池·多线程
智驱力人工智能11 分钟前
雨季智慧交通:从车辆盲区到客流统计的算法全覆盖
人工智能·科技·算法·安全·智慧城市·智慧交通·渣土车识别
凌佚25 分钟前
rknn优化教程(二)
c++·opencv·目标检测
·云扬·27 分钟前
【PmHub面试篇】PmHub中基于Redis加Lua脚本的计数器算法限流实现面试专题解析
redis·面试·lua
智驱力人工智能31 分钟前
大型活动交通拥堵治理的视觉算法应用
人工智能·算法·智慧城市·边缘计算·智慧交通·车牌识别算法·堵车识别算法
?!71443 分钟前
算法打卡17天(补)
c++·算法·职场和发展
邪恶的贝利亚1 小时前
WebRTC 中 ICE 流程优化:SRS 轻量级部署与 NAT 类型检测实战
c++·webrtc·流媒体·ice
YKPG1 小时前
C++学习-入门到精通【17】自定义的模板化数据结构
数据结构·c++·学习
会飞的架狗师2 小时前
【面试题】如何保证MQ的消息不丢失、不重复
java·面试·kafka·java-rocketmq