面试经典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;
    }
};
相关推荐
IT猿手11 分钟前
基于强化学习Q-learning算法的无人机三维路径规划算法原理与实现,MATLAB代码
算法·matlab·无人机·路径规划·动态路径规划
qq_4176950511 分钟前
C++中的解释器模式
开发语言·c++·算法
y = xⁿ17 分钟前
【LeetCodehot100】T108:将有序数组转换为二叉搜索树 T98:验证搜索二叉树
数据结构·算法·leetcode
程序员小崔日记30 分钟前
一道KMP统考真题彻底讲透:nextval与滑动距离的本质
算法·408·王道计算机考研
xiaoye-duck31 分钟前
《算法题讲解指南:动态规划算法--路径问题》--9.最小路径和,10.地下城游戏
c++·算法·动态规划
渡过晚枫40 分钟前
[第十四届蓝桥杯/java/算法]国赛A——跑步计划
算法
hanlin0343 分钟前
刷题笔记:力扣第17题-电话号码的字母组合
笔记·算法·leetcode
刺客xs1 小时前
c++模板
java·开发语言·c++
koping_wu1 小时前
常用中间件面试汇总:Mysql、Mq、Redis、操作系统、Nacos、Es、Mybatis
mysql·中间件·面试
不是株1 小时前
算 法
数据结构·python·算法