当前位置:网站首页>Leetcode advanced road - 125 Validate palindrome string
Leetcode advanced road - 125 Validate palindrome string
2022-06-10 21:22:00 【Li_ XiaoJin】
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
Example 2:
Input : "race a car"
Output : false
Learn a new method for this problem Character.isLetterOrDigit()java.lang.Character.isLetterOrDigit(char ch) Determines whether the specified character is alphabetic or numeric .java.lang.Character.isLetter(char ch) Make sure that the specified character is a letter .
/**
* 125. Verify the palindrome string
* @Author: lixj
* @Date: 2020/9/21 11:08
*/
public class ValidPalindrome {
/**
* Splice the new string and then judge , Double finger needling
* @param s
* @return
*/
public boolean isPalindrome(String s) {
char[] ch = s.toCharArray();
int len = s.length();
if (len == 0) return true;
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= len -1; i++) {
if (Character.isLetterOrDigit(ch[i])) {
sb.append(ch[i]);
}
}
int left = 0;
int right = sb.length() -1;
char[] chnew = sb.toString().toLowerCase().toCharArray();
while (left < right) {
if (chnew[left] != chnew[right]) {
return false;
}
left++;
right--;
}
return true;
}
/**
* The original string judgment
* @param s
* @return
*/
public boolean isPalindrome1(String s) {
int left = 0;
int right = s.length() -1;
while (left < right) {
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
++left;
}
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
--right;
}
if (left < right) {
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
return false;
}
}
++left;
--right;
}
return true;
}
public static void main(String[] args) {
String srt = "A man, a plan, a canal: Panama";
// String srt = "A man, a plan, a canal: vnama";
ValidPalindrome validPalindrome = new ValidPalindrome();
System.out.println(validPalindrome.isPalindrome1(srt));
}
}
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/leetcode Advanced road -125 Verify the palindrome string
边栏推荐
- Power consumption development experience sharing: design power consumption board
- Leetcode advanced road - plus one
- 一、Vulkan开发理论基础知识
- 视频监控系统存储控件,带宽计算方法
- ^30h5 web worker multithreading
- View play and earn will lead crypto games astray
- Leetcode advanced path - Search insertion location
- Practical | how to use burp suite for password blasting!
- A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
- Theoretical basis of distributed services
猜你喜欢

Serial Print() and serial The difference of write() function, and the problem of hexadecimal and string sending and receiving format in serial port communication and detailed explanation of the conver

Elastic-Job的快速入门,三分钟带你体验分布式定时任务

Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?

72. editing distance ●●

Pytorch deep learning -- convolution operation and code examples

Calculus review 1

Redis cache breakdown

35岁被裁员,还能拥有美妙人生吗?

1、 Vulkan develops theoretical fundamentals

^30h5 web worker multithreading
随机推荐
异步、线程池(CompletableFuture)
连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
Meetup Preview: introduction to the new version of linkis and the application practice of DSS
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
信号与系统复习1
Brute force method /k integers out of 1~n integers
Redis cache breakdown
自注意力(self-attention)和多头注意力(multi-head attention)
Leetcode advanced road - plus one
Understanding deep learning attention
You have to learn math to play art?
MySQL service startup failed
Use DAP link to download the executable file separately to the mm32f5 microcontroller
Codeforces Round #798 (Div. 2)
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
微积分复习1
2 pcs share a set of keyboard and mouse
分布式服务理论基础
CET-6 - Business English - the last recitation before the test
35岁被裁员,还能拥有美妙人生吗?