面试经典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;
    }
};
相关推荐
墨雪不会编程10 分钟前
数据结构—排序算法篇二
数据结构·算法·排序算法
ShineWinsu33 分钟前
对于数据结构:堆的超详细保姆级解析—上
数据结构·c++·算法·计算机·二叉树·顺序表·
im_AMBER1 小时前
Leetcode 46
c语言·c++·笔记·学习·算法·leetcode
QX_hao1 小时前
【Go】--文件和目录的操作
开发语言·c++·golang
卡提西亚1 小时前
C++笔记-20-对象特性
开发语言·c++·笔记
努力学算法的蒟蒻1 小时前
day09(11.6)——leetcode面试经典150
算法·leetcode·职场和发展
2301_796512521 小时前
Rust编程学习 - 内存分配机制,如何动态大小类型和 `Sized` trait
学习·算法·rust
三掌柜6662 小时前
C++ 零基础入门与冒泡排序深度实现
java·开发语言·c++
卿言卿语2 小时前
CC23-最长的连续元素序列长度
java·算法·哈希算法
沐怡旸3 小时前
【穿越Effective C++】条款15:在资源管理类中提供对原始资源的访问——封装与兼容性的平衡艺术
c++·面试