当前位置:网站首页>【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点告诉你实时聊天与聊天机器人组合的优势
- 424. 替换后的最长重复字符 ●●
- golang代码检查工具
- TVS管和ESD管的技术指标和选型指南-嘉立创推荐
- (4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
- MySQL replace primary key delete primary key add primary key
- Déterminer si un arbre binaire est un arbre binaire complet
- 2022.6.20-6.26 AI行业周刊(第103期):新的小生命
- STM32__06—单通道ADC
- Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
猜你喜欢
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
进击的技术er——自动化
无刷驱动设计——浅谈MOS驱动电路
Dynamic memory management (malloc/calloc/realloc)
Do you regret becoming a programmer?
Object detection based on impulse neural network
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
698. 划分为k个相等的子集 ●●
2:第一章:认识JVM规范1:JVM简介;
Debian 10 installation configuration
随机推荐
Idea connects to MySQL, and it is convenient to paste the URL of the configuration file directly
【LeetCode】5. Valid Palindrome·有效回文
Spire Office 7.5.4 for NET
Cwaitabletimer timer, used to create timer object access
帶外和帶內的區別
yate.conf
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
21.PWM应用编程
3D point cloud slam
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
无刷驱动设计——浅谈MOS驱动电路
YML configuration, binding and injection, verification, unit of bean
Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
4点告诉你实时聊天与聊天机器人组合的优势
How to improve eloquence
Go语言实现原理——锁实现原理
Pyqt control part (I)
Solution to the packaging problem of asyncsocket long connecting rod
保研笔记一 软件工程与计算卷二(1-7章)