当前位置:网站首页>Validate palindrome string
Validate palindrome string
2022-06-28 14:13:00 【Hua Weiyun】
title: Verify the palindrome string
date: 2022-04-25 14:21:32
categories: LeetCode
tags: Make a little progress every day
subject
difficulty Simple
Given a string , Verify that it is a palindrome string , Consider only alphabetic and numeric characters , The case of letters can be ignored .
explain : In this question , We define an empty string as a valid palindrome string .
Example 1:
Input : “A man, a plan, a canal: Panama”
Output : true
explain :“amanaplanacanalpanama” It's a palindrome stringExample 2:
Input : “race a car”
Output : false
explain :“raceacar” It's not a palindrome string
Tips :1 <= s.length <= 2 * 105
character string s from ASCII Character composition
Code :
class Solution { public boolean isPalindrome(String s) { // Convert all to lowercase first s = s.toLowerCase(); StringBuilder sCopy = new StringBuilder(); for (int i = 0; i < s.length(); i++) { // Self retaining letters and numbers if(Character.isLetterOrDigit(s.charAt(i))) { sCopy.append(s.charAt(i)); } } // Reverse string StringBuffer sCopy2 = new StringBuffer(sCopy).reverse(); // Compare return sCopy.toString().equals(sCopy2.toString()); }}C++ edition :
#include<bits/stdc++.h>using namespace std;class Solution {public: bool isPalindrome(string s) { string str; for(char ch : s){ // Consider only alphabetic and numeric characters if(isalnum(ch)){ // Convert characters to lowercase str += tolower(ch); } } /** * c.rbegin() Returns an inverse iterator , It points to the container c Last element of * c.rend() Returns an inverse iterator , It points to the container c The position in front of the first element of */ string str2(str.rbegin(), str.rend()); // Judge whether it is a palindrome string return str == str2; }};int main() { Solution s; cout << (s.isPalindrome("A man, a plan, a canal: Panama") ?"true":"false")<< endl; return 0;}The above is to verify the whole content of the palindrome string
Copyright notice :
Original Blogger : Cowherd Conan
Personal blog links :https://www.keafmd.top/
If it helps you , Thank you for clicking on == One key, three links == Support !
[ ha-ha ][ Huai Quan ]

come on. !
Joint efforts !
Keafmd
You can see it here , You know the following , Let's make progress together !
边栏推荐
- 同花顺上开户是安全的吗
- 3、项目的整体UI架构
- 外贸邮件推广怎么统计维度
- 2022金属非金属矿山安全检查(地下矿山)复训题库及在线模拟考试
- 你的代碼會說話嗎?(上)
- Deveco studio 3.0 editor configuration tips
- Reading notes of Mr. toad going to see a psychologist
- 配置文件加密(Jasypt的简单使用)
- Play NAS home NAS server setup scheme "suggestions collection"
- ArcGIS vector center point generates rectangle and cuts TIF image for deep learning sample training
猜你喜欢

PCB懂王,你是吗?我不是

How fragrant! The most complete list of common shortcut keys for pychar!

基于ASP的勤工俭学管理系统

Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系

If a programmer goes to prison, will he be assigned to write code?

Deveco studio 3.0 editor configuration tips

What is the progress of China open source with 7.55 million developers?

Yii2 writing the websocket service of swoole

Based on asp Net based document retrieval system

Four methods of thread termination
随机推荐
一个bug肝一周...忍不住提了issue
【二叉树】从叶结点开始的最小字符串
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
PCB懂王,你是吗?我不是
Foreign trade SEO Webmaster Tools
锐捷交换机配置ssh password登录命令[通俗易懂]
机器人的运动范围(DFS)
Based on asp Net based document retrieval system
[codec] write H264 decoder (1) from scratch
Tencent cloud international ECS has no network after logging in. How to troubleshoot?
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
[binary tree] allocate coins in the binary tree
PC博物馆-熟悉又陌生的懵懂年代
What is the progress of China open source with 7.55 million developers?
[experience sharing] summary of database operations commonly used in Django development
欧拉恒等式:数学史上的真正完美公式!
原生JS 实现页面元素的拖动 拖拽
How fragrant! The most complete list of common shortcut keys for pychar!
Jupyter notebook中添加虚拟环境
2022 questions d'examen pour les cuisiniers chinois (Senior) et l'examen de simulation en ligne