当前位置:网站首页>【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 *〜(ゝ.∂)
边栏推荐
- QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
- Basic knowledge of database (interview)
- Huawei simulator ENSP - hcip - MPLS experiment
- Idea rundashboard window configuration
- The interface of grafana tool displays an error, incluxdb error
- Spire Office 7.5.4 for NET
- 如何提升口才
- 2022.6.20-6.26 AI industry weekly (issue 103): new little life
- Leetcode buys and sells stocks
- 11gR2 Database Services for &quot;Policy&quot; and &quot;Administrator&quot; Managed Databases (文件 I
猜你喜欢
[classical control theory] summary of automatic control experiment
Huawei simulator ENSP - hcip - MPLS experiment
保研笔记一 软件工程与计算卷二(1-7章)
Initial experience | purchase and activate typora software
Rasa 3. X learning series -rasa x Community Edition (Free Edition) changes
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
MySQL replace primary key delete primary key add primary key
TVS管 与 稳压二极管参数对比
Do you regret becoming a programmer?
Go language implementation principle -- lock implementation principle
随机推荐
February 13, 2022 -5- maximum depth of binary tree
How to insert data into MySQL database- How can I insert data into a MySQL database?
Calculating the number of daffodils in C language
秒杀系统的设计与实现思路
Différence entre hors bande et en bande
Neural structured learning 4 antagonistic learning for image classification
LeetCode——Add Binary
保研笔记四 软件工程与计算卷二(8-12章)
帶外和帶內的區別
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
Practice of concurrent search
Solution to the packaging problem of asyncsocket long connecting rod
[Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
ORB_ SLAM2/3
JVM的简介
2022.6.20-6.26 AI industry weekly (issue 103): new little life
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
基于脉冲神经网络的物体检测
3D point cloud slam
开关电源Buck电路CCM及DCM工作模式