当前位置:网站首页>【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(),速度蹿蹿的~~
原来字符串的正则匹配这么慢,欢迎知道原因的同学评论区踢我哦*〜(ゝ。∂)
边栏推荐
- How to quickly understand complex businesses and systematically think about problems?
- Three. Js-01 getting started
- Negative sampling
- UVA11294-Wedding(2-SAT)
- Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
- asp.net弹出层实例
- Go language implementation principle -- lock implementation principle
- Leecode learning notes
- Non rigid / flexible point cloud ICP registration
- TVS管和ESD管的技術指標和選型指南-嘉立創推薦
猜你喜欢
进击的技术er——自动化
98. 验证二叉搜索树 ●●
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
[original] what is the core of programmer team management?
Three.js-01 入门
Matlab smooth curve connection scatter diagram
Scala concurrent programming (II) akka
Object detection based on impulse neural network
Using LNMP to build WordPress sites
随机推荐
The interface of grafana tool displays an error, incluxdb error
The method and principle of viewing the last modification time of the web page
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
Introduction to JVM
How to enable relationship view in phpMyAdmin - how to enable relationship view in phpMyAdmin
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
JVM的简介
Use of metadata in golang grpc
UVA – 11637 Garbage Remembering Exam (组合+可能性)
6-axis and 9-axis IMU attitude estimation
Go language implementation principle -- lock implementation principle
Three. JS VR house viewing
Leetcode sword finger offer brush questions - day 21
【原创】程序员团队管理的核心是什么?
How to design API return code (error code)?
yate.conf
LeetCode——Add Binary
Go语言实现原理——锁实现原理
媒体查询:引入资源
2: Chapter 1: understanding JVM specification 1: introduction to JVM;