当前位置:网站首页>验证回文串
验证回文串
2022-06-28 14:08:00 【华为云】
title: 验证回文串
date: 2022-04-25 14:21:32
categories: LeetCode
tags: 每天进步一点点
题目
难度 简单
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: “A man, a plan, a canal: Panama”
输出: true
解释:“amanaplanacanalpanama” 是回文串示例 2:
输入: “race a car”
输出: false
解释:“raceacar” 不是回文串
提示:1 <= s.length <= 2 * 105
字符串 s 由 ASCII 字符组成
代码:
class Solution { public boolean isPalindrome(String s) { //先都转小写 s = s.toLowerCase(); StringBuilder sCopy = new StringBuilder(); for (int i = 0; i < s.length(); i++) { //自保留字母和数字 if(Character.isLetterOrDigit(s.charAt(i))) { sCopy.append(s.charAt(i)); } } //反转字符串 StringBuffer sCopy2 = new StringBuffer(sCopy).reverse(); //比较 return sCopy.toString().equals(sCopy2.toString()); }}C++版:
#include<bits/stdc++.h>using namespace std;class Solution {public: bool isPalindrome(string s) { string str; for(char ch : s){ // 只考虑字母和数字字符 if(isalnum(ch)){ // 将字符转换为小写 str += tolower(ch); } } /** * c.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素 * c.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置 */ string str2(str.rbegin(), str.rend()); // 判断是否为回文串 return str == str2; }};int main() { Solution s; cout << (s.isPalindrome("A man, a plan, a canal: Panama") ?"true":"false")<< endl; return 0;}以上就是验证回文串的全部内容
版权声明:
原创博主:牛哄哄的柯南
个人博客链接:https://www.keafmd.top/
看完如果对你有帮助,感谢点击下面的==一键三连==支持!
[哈哈][抱拳]

加油!
共同努力!
Keafmd
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- Thread life cycle and its methods
- Luogu_ P1303 A*B Problem_ High precision calculation
- Why will the new 5g standard bring lower TCO to the technology stack
- Latest summary! 30 provinces announce 2022 college entrance examination scores
- 由两个栈组成的队列
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
- 2021计算机三级数据库大题总结
- 【二叉树】在二叉树中分配硬币
- (original) [Maui] realize "floating action button" step by step
- ThreadLocal的简单理解
猜你喜欢

Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects

Can your code talk? (upper)

行动诠释价值,城联优品韩董事长出席广东英德抗洪捐赠公益活动会

Thread life cycle and its methods

A bug liver a week I can't help mentioning issue

Four methods of thread termination

ArrayList源码解析

Reading notes of Mr. toad going to see a psychologist

PC Museum - familiar and strange ignorant age

坐拥755万开发者的中国开源,进度几何?
随机推荐
2022金属非金属矿山(地下矿山)主要负责人考试模拟100题模拟考试平台操作
ArrayList源码解析
基于MATLAB的混沌数字图像加密技术研究与仿真实现
众昂矿业着眼氟化工产业,布局新能源产业链
Kubernetes 深入理解Kubernetes(二) 声明组织对象
G : 最大流问题
2022下半年软考考试时间安排已确定!
做一个墨水屏电子钟,炫酷!
VPS是干嘛用的?有哪些知名牌子?与云服务器有什么区别?
Regular matching numbers, English and English symbols
What are the products of increased life insurance?
公司领导说,个人代码超10个Bug就开除,是什么体验?
Hematemesis recommends 17 "wheels" to improve development efficiency
Arcgis 矢量中心点生成矩形并裁剪tif图像进行深度学习样本训练
正则匹配数字,英文以及英文符号
[understanding of opportunity -32]: Guiguzi - Dui [x ī] Five attitudes towards danger and problems
由两个栈组成的队列
How to design data visualization platform
力扣解法汇总522-最长特殊序列 II
New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning