当前位置:网站首页>【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(),速度蹿蹿的~~

原来字符串的正则匹配这么慢,欢迎知道原因的同学评论区踢我哦*〜(ゝ。∂)
边栏推荐
- Fix the memory structure of JVM in one article
- What is the process of building a website
- (4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
- 无刷驱动设计——浅谈MOS驱动电路
- 并查集实践
- UVA – 11637 Garbage Remembering Exam (组合+可能性)
- Dynamic memory management (malloc/calloc/realloc)
- 98. 验证二叉搜索树 ●●
- Krypton Factor purple book chapter 7 violent solution
- Using LNMP to build WordPress sites
猜你喜欢

Go语言实现原理——Map实现原理

Expectation, variance and covariance

98. 验证二叉搜索树 ●●

Registration and skills of hoisting machinery command examination in 2022

Initial experience | purchase and activate typora software

Simple and beautiful method of PPT color matching

Selenium+pytest automated test framework practice

Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)

Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?

From the perspective of quantitative genetics, why do you get the bride price when you get married
随机推荐
ORB_ SLAM2/3
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
Go language implementation principle -- map implementation principle
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
MySQL (1) -- related concepts, SQL classification, and simple operations
(4) UART application design and simulation verification 2 - TX module design (stateless machine)
Selenium+Pytest自动化测试框架实战
How to insert data into MySQL database- How can I insert data into a MySQL database?
Judge whether the binary tree is a complete binary tree
Week 17 homework
Krypton Factor purple book chapter 7 violent solution
Scala concurrent programming (II) akka
C Primer Plus Chapter 9 question 9 POW function
2:第一章:认识JVM规范1:JVM简介;
Use of shell:for loop
UVA11294-Wedding(2-SAT)
判断二叉树是否为完全二叉树
Douban scoring applet Part-2
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
golang代码检查工具