当前位置:网站首页>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 !
边栏推荐
- Connected to rainwater series problems
- RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
- New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning
- 中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
- Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
- [codec] write H264 decoder (1) from scratch
- Native JS implements drag and drop of page elements
- 《蛤蟆先生去看心里医生》阅读笔记
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
- [experience sharing] summary of database operations commonly used in Django development
猜你喜欢

Reading notes of Mr. toad going to see a psychologist

接雨水系列问题

Work study management system based on ASP

中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”

Can your code talk? (upper)

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

荐书丨《大脑通信员》:如果爱情只是化学反应,那还能相信爱情吗?

PC Museum - familiar and strange ignorant age

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

药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
随机推荐
欧拉恒等式:数学史上的真正完美公式!
sort
iNFTnews | 科技巨头加快进军Web3和元宇宙
Navicat premium 16 permanent crack activation tool and installation tutorial (available for personal test)
N皇后问题
2022 questions d'examen pour les cuisiniers chinois (Senior) et l'examen de simulation en ligne
基于MATLAB的混沌数字图像加密技术研究与仿真实现
Rslo: self supervised lidar odometer (real time + high precision, icra2022)
设计一个有getMin功能的栈
【二叉树】在二叉树中分配硬币
荐书丨《大脑通信员》:如果爱情只是化学反应,那还能相信爱情吗?
你的代码会说话吗?(上)
Yii2 writing the websocket service of swoole
Open source invites you to participate in openinfra days China 2022. Topic collection is in progress ~
Hundreds of lines of code to implement a JSON parser
Four methods of thread termination
[codec] write H264 decoder (1) from scratch
Native JS implements drag and drop of page elements
Black apple installation tutorial OC boot "suggestions collection"
2022中式烹調師(高級)試題及在線模擬考試