面试经典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;
    }
};
相关推荐
Wadli6 分钟前
C++语法 | static静态|单例模式
开发语言·c++·单例模式
纪元A梦30 分钟前
贪心算法应用:K-Means++初始化详解
算法·贪心算法·kmeans
月阳羊31 分钟前
【硬件-笔试面试题-95】硬件/电子工程师,笔试面试题(知识点:RC电路中的时间常数)
java·经验分享·单片机·嵌入式硬件·面试
_不会dp不改名_44 分钟前
leetcode_21 合并两个有序链表
算法·leetcode·链表
进击的_鹏1 小时前
【C++11】initializer_list列表初始化、右值引用和移动语义、可变参数模版等
开发语言·c++
mark-puls1 小时前
C语言打印爱心
c语言·开发语言·算法
Python技术极客1 小时前
将 Python 应用打包成 exe 软件,仅需一行代码搞定!
算法
yinke小琪1 小时前
说说hashCode() 和 equals() 之间的关系
java·后端·面试
tongsound1 小时前
igh ethercat 实时性测试
linux·c++
吃着火锅x唱着歌1 小时前
LeetCode 3302.字典序最小的合法序列
leetcode