当前位置:网站首页>验证回文串
验证回文串
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
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- i++ , ++i
- Votre Code parle? (1)
- go数组与切片,[]byte转string[通俗易懂]
- Kubernetes 深入理解Kubernetes(二) 声明组织对象
- Arcgis 矢量中心点生成矩形并裁剪tif图像进行深度学习样本训练
- Euler equation: a truly perfect formula in the history of mathematics!
- Go array and slice, []byte to string[easy to understand]
- Websocket automatically disconnects in 1 minute
- MySQL从库Error:“You cannot ‘Alter‘ a log table...“
- Hundreds of lines of code to implement a JSON parser
猜你喜欢

Source code analysis of ArrayList

欧拉恒等式:数学史上的真正完美公式!

众昂矿业着眼氟化工产业,布局新能源产业链

Mulan open work license 1.0 open to the public for comments

Tencent cloud international ECS has no network after logging in. How to troubleshoot?

Euler equation: a truly perfect formula in the history of mathematics!

PostgreSQL surpasses MySQL

RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)

公司领导说,个人代码超10个Bug就开除,是什么体验?

Why can't Bert completely kill the BM25??
随机推荐
正则匹配数字,英文以及英文符号
为什么新的5G标准将为技术栈带来更低的 TCO
Npoi export excel and download to client
基于MATLAB的混沌数字图像加密技术研究与仿真实现
go数组与切片,[]byte转string[通俗易懂]
[codec] write H264 decoder (1) from scratch
30 sets of JSP website source code collection "suggestions collection"
Simple understanding of ThreadLocal
Play NAS home NAS server setup scheme "suggestions collection"
Reading notes of Mr. toad going to see a psychologist
Euler equation: a truly perfect formula in the history of mathematics!
Reverse a stack with recursive functions and stack operations only
排序
Mulan open work license 1.0 open to the public for comments
Kubernetes in-depth understanding of kubernetes (I)
仅用递归函数和栈操作逆序一个栈
Yii2 writing the websocket service of swoole
Double buffer drawing
接雨水系列问题
Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market