当前位置:网站首页>Leetcode 0125. validate palindrome string
Leetcode 0125. validate palindrome string
2022-07-24 23:56:00 【Tisfy】
【LetMeFly】125. Verify the palindrome string
Force button topic link :https://leetcode.cn/problems/valid-palindrome/
Given a string , Verify that it is a palindrome string , Consider only alphabetic and numeric characters , The case of letters can be ignored .
explain : In this question , We define an empty string as a valid palindrome string .
Example 1:
Input : "A man, a plan, a canal: Panama" Output : true explain :"amanaplanacanalpanama" It's a palindrome string
Example 2:
Input : "race a car" Output : false explain :"raceacar" It's not a palindrome string
Tips :
1 <= s.length <= 2 * 105- character string
sfrom ASCII Character composition
Method 1 : Clean string + Judge
This method is easy to think of , But the space complexity is slightly larger
Since the topic says only Alphanumeric , Then why don't we extract the alphanumeric , Form a new string .
string strip(string& s) {
string ans;
for (char& c : s) {
if (c >= 'a' && c <= 'z')
ans += c;
else if (c >= 'A' && c <= 'Z') // The words of capital letters will be converted into lowercase ( This question is not case sensitive )
ans += (char)(c + 32);
else if (c >= '0' && c <= '9')
ans += c;
}
return ans;
}
after , Then judge whether the cleaned string is a palindrome string ( From front to back, the variable is half a character , Look at number i i i One and last i i i Whether the characters are the same )
bool isPalindrome(string& s) {
s = strip(s);
int n = s.size();
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - i - 1])
return false;
}
return true;
}
- Time complexity O ( N ) O(N) O(N), among N N N Is the length of the string
- Spatial complexity O ( N ) O(N) O(N)
AC Code
C++
class Solution {
private:
string strip(string& s) {
string ans;
for (char& c : s) {
if (c >= 'a' && c <= 'z')
ans += c;
else if (c >= 'A' && c <= 'Z')
ans += (char)(c + 32);
else if (c >= '0' && c <= '9')
ans += c;
}
return ans;
}
public:
bool isPalindrome(string& s) {
s = strip(s);
int n = s.size();
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - i - 1])
return false;
}
return true;
}
};
Method 2 : No extra space , Ignore non alphanumeric characters directly
In this way, it is impossible to directly determine the first i i i The penultimate number corresponding to an alphanumeric i i i Who are the first alphanumeric .
Then use double pointers directly , One before and one after , One goes back and forth , Ignore when encountering non alphanumeric , Until the front and back pointers overlap
- Time complexity O ( N ) O(N) O(N), among N N N Is the length of the string
- Spatial complexity O ( 1 ) O(1) O(1)
AC Code
C++
class Solution {
private:
inline bool isCN(char c) {
if (c >= 'A' && c <= 'Z')
return true;
if (c >= 'a' && c <= 'z')
return true;
if (c >= '0' && c <= '9')
return true;
return false;
}
public:
bool isPalindrome(string s) {
int l = 0, r = s.size() - 1;
while (l < r) {
// Find the next alphanumeric
while (!isCN(s[l]) && l < r)
l++;
while (!isCN(s[r]) && l < r)
r--;
// Alphabetic words are uniformly converted to lowercase
if (s[l] >= 'A' && s[l] <= 'Z')
s[l] += 32;
if (s[r] >= 'A' && s[r] <= 'Z')
s[r] += 32;
printf("l = %d, r = %d, s[%d] = %c, s[%d] = %c\n", l, r, l, s[l], r, s[r]); //**********
if (s[l] != s[r])
return false;
l++, r--;
}
return true;
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125889961
边栏推荐
- Yaml writing rules and comparison between yaml and JSON
- 线段树杂谈
- How to propose effective solutions for high-end products? (1 methodology + 2 cases + 1 List)
- 你还在使用System.currentTimeMillis()?来看看StopWatch吧
- Sql文件导入数据库-保姆级教程
- See project code Note 1
- Js----- Chapter 4 array
- Is the income of CICC securities' new financial products 6%? I want to open an account and manage money
- Zheng Huijuan: Research on application scenarios and evaluation methods of data assets based on the unified market
- Collection of common online testing tools
猜你喜欢

技术操作

ShardingSphere-数据库分库分表简介

Introduction to HLS programming

Redis memory analysis tool RMA usage

Notes of Teacher Li Hongyi's 2020 in-depth learning series 8

See project code Note 1

Notes of Teacher Li Hongyi's 2020 in-depth learning series 3

salesforce零基础学习(一百一十六)workflow -&gt; flow浅谈

C语言学习之分支与循环语句

Technical operation
随机推荐
dpkg : Breaks: libapt-pkg5.0 (< 1.7~b) but 1.6.15 is to be installedE: Broken packages
必会面试题:1.浅拷贝和深拷贝_浅拷贝
Transmission download list, download file migration machine guide
Qt项目-安防监控系统(各个界面功能实现)
Answers to some problems encountered in the process of Xperia XZ (f8332) brushing and root
多线程&高并发(全网最新:面试题 + 导图 + 笔记)面试手稳心不慌
云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
采坑记录:TypeError: 'module' object is not callable
Video chat source code - one-to-one live broadcast system source code
Notes of Teacher Li Hongyi's 2020 in-depth learning series 8
芯片的功耗
Understanding complexity and simple sorting operation
Notes of Teacher Li Hongyi's 2020 in-depth learning series 5
cloud chart
给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
Entity service is an anti pattern
技术操作
Mandatory interview questions: 1. shallow copy and deep copy_ Deep copy
[nuxt 3] (x) runtime configuration
1. Smoke test