面试经典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;
    }
};
相关推荐
徐某人..8 分钟前
基于i.MX6ULL平台的智能网关系统开发
arm开发·c++·单片机·qt·物联网·学习·arm
无敌秋44 分钟前
# C++ 简单工厂模式实战指南
c++·简单工厂模式
木斯佳1 小时前
前端八股文面经大全:字节抖音前端三面(2026-04-27)·面经深度解析
前端·面试·笔试·八股·面经
code_pgf1 小时前
Octo 算法详解-开源通用机器人策略模型技术报告
算法·机器人·开源
cany10001 小时前
C++ -- 模板的声明和定义
开发语言·c++
澈2071 小时前
深耕进阶 Day1:C 与 C++ 核心差异 + C++ 入门基石
c语言·开发语言·c++
嘻嘻哈哈樱桃1 小时前
牛客经典101题题解集--动态规划
java·数据结构·python·算法·职场和发展·动态规划
脱氧核糖核酸__1 小时前
LeetCode热题100——234.回文链表(两种解法)
c++·算法·leetcode·链表
IronMurphy1 小时前
【算法四十二】118. 杨辉三角 198. 打家劫舍
算法
电科一班林耿超1 小时前
第 16 课:动态规划专题(二)—— 子序列与子数组问题:面试最高频的 DP 题型
数据结构·算法·动态规划