当前位置: 首页 > article >正文

验证回文串 - 简单

*************

c++

topc:LCR 018. 验证回文串 - 力扣(LeetCode)

*************

Sometime really donnot want to write things just because of donot want to. Make excuses like not feel good or have no time. 

Inspect the topic first:

The first question is how to transelate uppercases to lowercases. 

So I refer to some of my frienmds and they told me in c++, a standard usage is here

int tolower(int c);

and if you want to turn the letters to uppercases, that is easy:

int toupper(int c);

why need to transform the letters? because in another case here125. 验证回文串 - 力扣(LeetCode)125. 验证回文串 - 如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。字母和数字都属于字母数字字符。给你一个字符串 s,如果它是 回文串 ,返回 true ;否则,返回 false 。 示例 1:输入: s = "A man, a plan, a canal: Panama"输出:true解释:"amanaplanacanalpanama" 是回文串。示例 2:输入:s = "race a car"输出:false解释:"raceacar" 不是回文串。示例 3:输入:s = " "输出:true解释:在移除非字母数字字符之后,s 是一个空字符串 "" 。由于空字符串正着反着读都一样,所以是回文串。 提示: * 1 <= s.length <= 2 * 105 * s 仅由可打印的 ASCII 字符组成icon-default.png?t=O83Ahttps://leetcode.cn/problems/valid-palindrome/

need to transform uppercases and lowercases. the rest of them totally same.

Then I need to pointers i and j. pointrt i travels from left to right. pointer j travels from right to left at the same time. check tolower[i] != tolower[j]. If they say yes everytime, then comes true.

class Solution {
public:
    bool isPalindrome(std::string s) {
        int i = 0;
        int j = s.length() - 1;

            // 比较字符,忽略大小写
            if (tolower(s[i]) != tolower(s[j])) {
                return false;
            i++;
            j--;
        }
        
        return true;
    }
};

 

It cannot run.  need to skip the space and punctuation:

class Solution {
public:
    bool isPalindrome(string s) {

        int i = 0;
        int j = s.length() - 1;
        
        while (i < j) {
            // 跳过非字母数字字符
            if (!isalnum(s[i])) {
                i++;
                continue;
            }
            if (!isalnum(s[j])) {
                j--;
                continue;
            }
            
            // 比较字符,忽略大小写
            if (tolower(s[i]) != tolower(s[j])) {
                return false;
            }
            
            i++;
            j--;
        }
        
        return true;
        
    }
};

here, isalnum is a standard library. isalnum library to check if the input is numbers or letters. Think you receive a verifiction code, input the code.

!isalnum(s[j]) This expression checks if s[j] is not an alpha or numeric character. It returns true if s[j] is not an alpha or numeric, and false if it is, and is often used in string processing to skip non-alphanumeric characters.


http://www.kler.cn/a/451910.html

相关文章:

  • 复习打卡大数据篇——Hadoop HDFS 03
  • 一款5k star的 Redis 客户端!!简洁高效!
  • zabbix监控山石系列Hillstone监控模版(适用于zabbix7及以上)
  • 菜鸟带新鸟——基于EPlan2022的部件库制作
  • 串口通信控制LED灯
  • shardingsphere分库分表项目实践1-让shardingsphere运行起来
  • Text组件的用法
  • 【vue】vite + ts +vue3 安装pinia
  • 创新技术集成:Web机票管理系统的未来趋势
  • 基于CNN-BiLSTM-selfAttention混合神经网络的多分类预测【MATLAB】
  • C语言从入门到放弃教程
  • MFC/C++学习系列之简单记录12——文件操作
  • GitFlow工作流
  • Batch_Size对神经网络训练效率的影响:一个PyTorch实例分析
  • JAVA智慧养老养老护理帮忙代办陪诊陪护小程序APP源码
  • 2024-12-25-sklearn学习(20)无监督学习-双聚类 料峭春风吹酒醒,微冷,山头斜照却相迎。
  • Java 中 getClass() 方法的使用与原理分析:深入理解对象类型信息
  • [C/C++]智能指针是什么?实现原理是什么?
  • 鸿蒙设置app更新跳转华为市场
  • 一个桌面工具条系统,插件一键启动,快速扩展提高工作效率
  • 硬件设计:RS232电平标准
  • 如何在谷歌浏览器中设置默认下载路径
  • R基于贝叶斯加法回归树BART、MCMC的DLNM分布滞后非线性模型分析母婴PM2.5暴露与出生体重数据及GAM模型对比、关键窗口识别
  • 集合stream
  • springboot/ssm社区助老志愿者服务平台Java代码编写web志愿捐赠活动项目
  • Linux文件目录 --- touch命令创建文件