[LeetCode]125. Valid Palindrome

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

[LeetCode]125. Valid Palindrome

题目描述

这里写图片描述

思路

1.筛选去掉无用字符
2.判断当前字符串是否回文串

代码

#include <iostream>
#include <string>

using namespace std;

class Solution {
public:
    bool isPalindrome(string s) {
        /*
        int src = 0, cur = 0;
        while (src < s.size()) {
            if ((s[src] >= '0' && s[src] <= '9') || (s[src] >= 'a' && s[src] <= 'z') || (s[src] >= 'A' && s[src] <= 'Z')) {
                if (s[src] >= 'A' && s[src] <= 'Z')
                    s[cur++] = s[src++] - ('A' - 'a');
                else
                    s[cur++] = s[src++];
            }
            else
                ++src;
        }
        s.resize(cur);
        int start = 0, end = s.size();
        while (start < end) {
            if (s[start] == s[end - 1]) {
                start++;
                end--;
            }
            else
                return false;
        }
        return true;
        */
        for (int i = 0, j = s.size() - 1; i < j; i++, j--) { // Move 2 pointers from each end until they collide
            while (isalnum(s[i]) == false && i < j) i++; // Increment left pointer if not alphanumeric
            while (isalnum(s[j]) == false && i < j) j--; // Decrement right pointer if no alphanumeric
            if (toupper(s[i]) != toupper(s[j])) return false; // Exit and return error if not match
        }

        return true;
    }
};

int main() {
    Solution s;

    cout << s.isPalindrome("race a car") << endl;

    system("pause");
    return 0;
}
LeetCode 125. Valid Palindrome LeetCode 125. Valid Palindrome Description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring other cases. For example, “A man, a plan, a cana 阅读详情

相关推荐

LeetCode刷题:125. Valid Palindrome

LeetCode刷题:125. Valid Palindrome 原题链接:https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. N...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 182

LeetCode力扣之125. Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindro...

LeeWei 327

[leetcode]125. Valid Palindrome

[leetcode]125. Valid Palindrome Analysis 周五啦周五啦,网上买的小南瓜到啦—— [六一快乐,嘻嘻~] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: Fo...

weixin_32135877的博客 125

LeetCode: 125. Valid Palindrome

LeetCode: 125. Valid Palindrome 题目描述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example: &quot;A man, a plan, a canal: Panama&quot; is ...

杨领well的专栏 696

leetcode 125. Valid Palindrome

leetcode 125. Valid Palindrome 题目大意:判断一句话时候为回文,只考虑字母,忽略大小写。 解题思路: 1、判断一个字符是否为数字或者字母 2、在比较的时候不分大小写,因此大写直接全部转为小写 3、在判断回文时直接比较首尾的两个字符

编码之路 295

LeetCode125Valid Palindrome

LeetCode125Valid Palindrome

上善若水 800

LeetCode125Valid Palindrome

Runtime:8 ms, faster than85.46%ofC++online submissions forValid Palindrome. Memory Usage:9.4 MB, less than61.22%ofC++online submissions forValid Palindrome. class Solution { public: ...

weixin_39458342的博客 151

LeetCode力扣 125. 验证回文串 Valid Palindrome 题解代码 JavaScript

问题https://leetcode-cn.com/problems/valid-palindrome/ 练习使用JavaScript解答 /** * @param {string} s * @return {boolean} */ function judgeChar(c) { if(c >= '0' && c <= '9') return true; if(c >= 'a' && c <= 'z') .

Coding home - 漂流瓶jz 460

leetcode】【125Valid Palindrome

一、问题描述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a ca

斗code、年华 538

leetCodeleetCode125 Valid Palindrome

题目如下: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. ...

随手积累,典型bug解决。技术栈的系统介绍。 193

LeetCode 125 Valid Palindrome

LeetCode 125 Valid Palindrome Problem Description: 判断给出的字符串是否是有效的回文字符串。 具体的题目信息: https://leetcode.com/problems/valid-palindrome/description/ Solution: 解题思路:用两个变量i和j从字符串两端向中间移动,比较两个变量指向的字符 (1)如...

萤火虫啊飞呀飞 279

leetcode125. Valid Palindrome

125.Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note:For the purpose of this problem, we define empty string as v...

lemonade13的博客 122

LeetCode 125. Valid Palindrome 题解

本题是字符串处理的经典问题。双指针技巧字符类型判断大小写处理边界条件处理通过本题可以深入理解双指针在字符串处理中的应用。

cannonjinx的博客 3115

【回文串3】LeetCode 125. Valid Palindrome

LeetCode 125. Valid Palindrome Solution1:我的答案 复杂度为O(n)O(n)O(n),写法不是很简练啊。 class Solution { public: bool isPalindrome(string s) { int n = s.size(); if (n &amp;lt;= 1) return true;...

Allenlzcoder的博客 278

[Leetcode] 125. Valid Palindrome

Problems: https://leetcode.com/problems/valid-palindrome/ Solution: class Solution { public boolean isPalindrome(String s) { // 将字符小写化 String ls = s.toLowerCase(); // 删除不是数字/字...

Renoler的博客 109

LeetCode125. Valid Palindrome

LeetCode125 传送门 解法比较简单: 两个指针,分别从左向右,从右向左遍历字符串,时间复杂度,空间复杂度。 Python源码: class Solution: def isPalindrome(self, s: str) -> bool: s = s.lower() left_p = 0 right_p = ...

weixin_42344158的博客 136
上一篇: [LeetCode]69. Sqrt(x)
下一篇: [LeetCode]168. Excel Sheet Column Title
charon____
博客等级 码龄11年 3粉丝 202原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值