当前位置:网站首页>【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(),速度蹿蹿的~~
原来字符串的正则匹配这么慢,欢迎知道原因的同学评论区踢我哦*〜(ゝ。∂)
边栏推荐
- Detailed explanation of pointer and array written test of C language
- Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
- (4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
- Initial experience | purchase and activate typora software
- Selenium+Pytest自动化测试框架实战
- TVS管和ESD管的技术指标和选型指南-嘉立创推荐
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
- Go语言实现原理——Map实现原理
- 代码农民提高生产力
- 2022.6.20-6.26 AI行业周刊(第103期):新的小生命
猜你喜欢
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
Three. JS VR house viewing
Sum of two numbers, sum of three numbers (sort + double pointer)
TypeError: this. getOptions is not a function
698. 划分为k个相等的子集 ●●
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
【原创】程序员团队管理的核心是什么?
并查集实践
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
随机推荐
Krypton Factor-紫书第七章暴力求解
asp.net弹出层实例
AsyncSocket长连接棒包装问题解决
Scala concurrent programming (II) akka
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Negative sampling
Practice of concurrent search
Common static methods of math class
C Primer Plus Chapter 9 question 10 binary conversion
UVA – 11637 garbage remembering exam (combination + possibility)
424. 替换后的最长重复字符 ●●
Week 17 homework
How to enable relationship view in phpMyAdmin - how to enable relationship view in phpMyAdmin
Development specification: interface unified return value format [resend]
UVA11294-Wedding(2-SAT)
poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
(4) UART application design and simulation verification 2 - RX module design (stateless machine)
Leecode learning notes
YML configuration, binding and injection, verification, unit of bean
6-axis and 9-axis IMU attitude estimation