当前位置:网站首页>【LeetCode】5. Valid palindrome
【LeetCode】5. Valid palindrome
2022-07-05 23:34:00 【AQin1012】
Title Description
Description in English
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise.
English address
https://leetcode.com/problems/valid-palindrome/
https://leetcode.com/problems/valid-palindrome/
Description of Chinese version
Given a string s , verification s Whether it is Palindrome string , Consider only alphabetic and numeric characters , The case of letters can be ignored . In this question , Define an empty string as a valid Palindrome string .
Example 1: Input : s = "A man, a plan, a canal: Panama" Output : true explain :"amanaplanacanalpanama" It's a palindrome string Example 2: Input : s = "race a car" Output : false explain :"raceacar" It's not a palindrome string
Constraints· Tips :
1 <= s.length <= 2 * 105
character string s from ASCII Character composition
Address of Chinese version
https://leetcode.cn/problems/XltzEq/
https://leetcode.cn/problems/XltzEq/
Their thinking
First turn all lowercase letters ( The title requires that case be ignored ) Then remove the characters except letters and numbers , Compare first and last
How to solve the problem
My version

class Solution {
public static boolean isPalindrome(String s) {
String s1 = s.toLowerCase().replaceAll("[^a-z|0-9]", "");
int length = s1.length();
for (int i = 0; i < length; i++) {
int j = length - i - 1;
if(j <= i){
break;
}
char c1 = s1.charAt(i);
char c2 = s1.charAt(j);
if (c1 != c2) {
return false;
}
}
return true;
}
}Official edition
Screening + Compare

class Solution {
public boolean isPalindrome(String s) {
StringBuffer sgood = new StringBuffer();
int length = s.length();
for (int i = 0; i < length; i++) {
char ch = s.charAt(i);
if (Character.isLetterOrDigit(ch)) {
sgood.append(Character.toLowerCase(ch));
}
}
StringBuffer sgood_rev = new StringBuffer(sgood).reverse();
return sgood.toString().equals(sgood_rev.toString());
}
}
Double finger needling

class Solution {
public boolean isPalindrome(String s) {
StringBuffer sgood = new StringBuffer();
int length = s.length();
for (int i = 0; i < length; i++) {
char ch = s.charAt(i);
if (Character.isLetterOrDigit(ch)) {
sgood.append(Character.toLowerCase(ch));
}
}
int n = sgood.length();
int left = 0, right = n - 1;
while (left < right) {
if (Character.toLowerCase(sgood.charAt(left)) != Character.toLowerCase(sgood.charAt(right))) {
return false;
}
++left;
--right;
}
return true;
}
}summary
My method seems to be very similar to the official double pointer method , But the execution time and memory consumption are more , Especially the execution time 》〉 As expected, there is no harm without comparison (╯﹏╰)

At first, I thought it was the one I used in the later comparison String, Official StringBuffer,StringBuffer Than String The reason why traversal is fast , So I assigned the regular matching string in my code to a StringBuffer, And then use that StringBuffer Traversal , But it is of no damn use (-。-; More slowly ...

But! When I replace regular matching with Character.isLetterOrDigit(), The speed is soaring ~~

The regular matching of the original string is so slow , Welcome students who know the reason to kick me in the comment area *〜(ゝ.∂)
边栏推荐
- (4) UART application design and simulation verification 2 - TX module design (stateless machine)
- LeetCode——Add Binary
- LeetCode——Add Binary
- Difference between out of band and in band
- Summary of binary tree recursive routines
- 698. Divided into k equal subsets ●●
- 开关电源Buck电路CCM及DCM工作模式
- 进击的技术er——自动化
- Qcombox (rewrite) + qcompleter (auto completion, auto loading the drop-down options of qcombox, setting the background color)
- The interface of grafana tool displays an error, incluxdb error
猜你喜欢

Go language implementation principle -- lock implementation principle

Pyqt control part (I)

From the perspective of quantitative genetics, why do you get the bride price when you get married

Xinyuan & Lichuang EDA training camp - brushless motor drive

Spire Office 7.5.4 for NET
![[classical control theory] summary of automatic control experiment](/img/22/9c9e107da7e305ce0a57d55b4d0b5a.png)
[classical control theory] summary of automatic control experiment

Neural structured learning 4 antagonistic learning for image classification

orgchart. JS organization chart, presenting structural data in an elegant way

Rasa 3.x 学习系列-Rasa X 社区版(免费版) 更改

Non rigid / flexible point cloud ICP registration
随机推荐
LeetCode——Add Binary
Opencvsharp (C openCV) shape detection and recognition (with source code)
AsyncSocket长连接棒包装问题解决
asp. Net pop-up layer instance
Judge whether the binary tree is a complete binary tree
6-axis and 9-axis IMU attitude estimation
2:第一章:认识JVM规范1:JVM简介;
424. 替换后的最长重复字符 ●●
Go语言实现原理——Map实现原理
Do you regret becoming a programmer?
Difference between out of band and in band
regular expression
【LeetCode】5. Valid Palindrome·有效回文
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
February 13, 2022 -5- maximum depth of binary tree
Summary of binary tree recursive routines
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
Calculating the number of daffodils in C language