面试经典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;
    }
};
相关推荐
_日拱一卒4 分钟前
LeetCode:240搜索二维矩阵Ⅱ
数据结构·线性代数·leetcode·矩阵
WolfGang0073215 分钟前
代码随想录算法训练营 Day33 | 动态规划 part06
算法·leetcode·动态规划
aini_lovee5 分钟前
半定规划(SDP)求解的 MATLAB 实现
算法
阳火锅9 分钟前
Element / AntD 官方都没做好的功能,被这个开源小插件搞定了!
前端·vue.js·面试
米粒116 分钟前
力扣算法刷题 Day 41(买卖股票)
算法·leetcode·职场和发展
幻风_huanfeng18 分钟前
人工智能之数学基础:内点法和外点法的区别和缺点
人工智能·算法·机器学习·内点法·外点法
天若有情67320 分钟前
颠覆C++传统玩法!Property属性与伪类,开辟静态语言新维度
java·c++·servlet
何陋轩22 分钟前
Netty高性能网络编程深度解析:把网络框架核心讲透,让面试官刮目相看
后端·面试
MIngYaaa52026 分钟前
The 6th Liaoning Provincial Collegiate Programming Contest - External 复盘
算法
CylMK28 分钟前
题解:P11625 [迷宫寻路 Round 3] 迷宫寻路大赛
c++·数学·算法