当前位置:网站首页>【LeetCode】5. Valid Palindrome·有效回文
【LeetCode】5. Valid Palindrome·有效回文
2022-07-05 23:08:00 【AQin1012】
题目描述
英文版描述
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.
英文版地址
https://leetcode.com/problems/valid-palindrome/
https://leetcode.com/problems/valid-palindrome/
中文版描述
给定一个字符串 s ,验证 s 是否是 回文串 ,只考虑字母和数字字符,可以忽略字母的大小写。 本题中,将空字符串定义为有效的 回文串 。
示例 1: 输入: s = "A man, a plan, a canal: Panama" 输出: true 解释:"amanaplanacanalpanama" 是回文串 示例 2: 输入: s = "race a car" 输出: false 解释:"raceacar" 不是回文串
Constraints·提示:
1 <= s.length <= 2 * 105
字符串 s 由 ASCII 字符组成
中文版地址
https://leetcode.cn/problems/XltzEq/
https://leetcode.cn/problems/XltzEq/
解题思路
先全部转小写字母(题目要求忽略大小写)然后先去除除了字母和数字的字符,首尾依次比较
解题方法
俺这版

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;
}
}官方版
筛选+比较

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());
}
}
双指针法

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;
}
}总结
我的方法看似和官方提供的双指针法很像,但是执行时间和内存消耗都要更多,特别是执行时间 》〉果然没有对比就没有伤害(╯﹏╰)

刚开始我以为是后面比较时我用的String,官方用的StringBuffer,StringBuffer比String遍历快的原因,于是我把我代码中正则匹配后的的字符串赋值给了一个StringBuffer,然后使用该StringBuffer进行遍历,然并卵(-。-; 更慢了。。。

But!当我把正则匹配换成了Character.isLetterOrDigit(),速度蹿蹿的~~

原来字符串的正则匹配这么慢,欢迎知道原因的同学评论区踢我哦*〜(ゝ。∂)
边栏推荐
- 3:第一章:认识JVM规范2:JVM规范,简介;
- Design and implementation of secsha system
- The method and principle of viewing the last modification time of the web page
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
- Selenium+Pytest自动化测试框架实战
- Development specification: interface unified return value format [resend]
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
- Solution to the packaging problem of asyncsocket long connecting rod
- Getting started stm32--gpio (running lantern) (nanny level)
- 2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
猜你喜欢

Go language implementation principle -- lock implementation principle

无刷驱动设计——浅谈MOS驱动电路

TVS管 与 稳压二极管参数对比

Dynamic memory management (malloc/calloc/realloc)

TVS管和ESD管的技術指標和選型指南-嘉立創推薦

东南亚电商指南,卖家如何布局东南亚市场?

Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)

Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d

Expectation, variance and covariance

3:第一章:认识JVM规范2:JVM规范,简介;
随机推荐
Three. JS VR house viewing
Object detection based on impulse neural network
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
Marginal probability and conditional probability
yate.conf
TVS管和ESD管的技術指標和選型指南-嘉立創推薦
Selenium+pytest automated test framework practice
Development specification: interface unified return value format [resend]
Douban scoring applet Part-2
UVA – 11637 garbage remembering exam (combination + possibility)
Three. Js-01 getting started
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
UVA11294-Wedding(2-SAT)
Detailed explanation of pointer and array written test of C language
Multi camera stereo calibration
MySQL (1) -- related concepts, SQL classification, and simple operations
11gR2 Database Services for &quot;Policy&quot; and &quot;Administrator&quot; Managed Databases (文件 I
Introduction to JVM
Neural structured learning - Part 2: training with natural graphs
Leetcode sword finger offer brush questions - day 21