面试经典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;
    }
};
相关推荐
wabs6661 小时前
关于贪心算法的思考
算法·贪心算法
社交怪人1 小时前
【判断大小】信息学奥赛一本通C语言解法(题号1043)
算法
Snasph2 小时前
GNU Make 用户手册(中文版)
服务器·算法·gnu
江澎涌2 小时前
拆解与 AI 的一次对话
人工智能·算法·程序员
sheeta19982 小时前
LeetCode 每日一题笔记 日期:2026.06.02 题目:3635. 最早完成陆地和水上游乐设施的时间 II
笔记·算法·leetcode
Lsk_Smion3 小时前
力扣实训 _ [102].层序遍历--前序--后续_递归与非递归的实现
数据结构·算法·leetcode
Lsk_Smion3 小时前
力扣实训 _ [25].K个一组链表
数据结构·链表
小欣加油4 小时前
leetcode3751 范围内总波动值I
java·数据结构·c++·算法·leetcode
代码中介商4 小时前
C++左值与右值:核心判断法则详解
开发语言·c++
JAVA9654 小时前
JAVA面试-并发篇 05-并发包AQS队列实现原理是什么
java·开发语言·面试