leetcode 125. Valid Palindrome ----- java

时间:2022-02-20 03:26:46

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

判读一个字符串是否是回文字符串(只判断期中的字符和数字)。

1、两个栈,从前向后和从后向前,然后判断两个栈是否相同。

public class Solution {
public boolean isPalindrome(String s) {
Stack stack1 = new Stack<Character>();
Stack stack2 = new Stack<Character>();
s = s.toLowerCase();
char[] word = s.toCharArray();
int len = s.length();
for( int i = 0;i<len;i++){
if( (word[i] >= '0' && word[i] <= '9') || (word[i]>='a' && word[i]<='z') )
stack1.push(word[i]);
if( (word[len-1-i] >= '0' && word[len-1-i] <= '9') || (word[len-1-i]>='a' && word[len-1-i]<='z') )
stack2.push(word[len-1-i]);
} return stack1.equals(stack2); }
}

2、直接用两个指针记录left和right即可。

public class Solution {
public boolean isPalindrome(String s) { char[] word = s.toLowerCase().toCharArray();
int len = s.length();
int left = 0,right = len-1;
while( left < right ){ if( !((word[left] >= '0' && word[left] <= '9') || (word[left]>='a' && word[left]<='z' )) )
left++;
else if( !((word[right] >= '0' && word[right] <= '9') || (word[right]>='a' && word[right]<='z' )) )
right--;
else if( word[left] == word[right]){
left++;
right--;
}else
return false;
}
return true; }
}

leetcode 125. Valid Palindrome ----- java的更多相关文章

  1. Java &lbrack;Leetcode 125&rsqb;Valid Palindrome

    题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  2. &lbrack;LeetCode&rsqb; 125&period; Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. LeetCode 125&period; Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

  4. &lbrack;leetcode&rsqb;125&period; Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. LeetCode 125 Valid Palindrome(有效回文)(&ast;)

    翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...

  6. Java for LeetCode 125 Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. Leetcode 125 Valid Palindrome 字符串处理

    题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...

  8. &lbrack;LeetCode&rsqb; 680&period; Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  9. 125&period; Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

随机推荐

  1. Dicom格式文件解析器

    转自:http://www.cnblogs.com/assassinx/archive/2013/01/09/dicomViewer.html Dicom全称是医学数字图像与通讯,这里讲的暂不涉及通讯 ...

  2. Objective-C基础笔记(2)&commat;property和&commat;synthesize

    先贴出使用@property和@synthesize实现的上一篇中的代码,再解释这两个keyword的使用方法和含义,代码例如以下: Person.h文件 #import <Foundation ...

  3. android 自定义百度地图放大缩小

    自定义实现Android百度地图的缩放图标,需要自定义一个缩放控件,实现效果如下: 这里的缩放效果,实现了点击按钮可以对地图的放大缩小,通过手势放大与缩小也控制缩放图标的可用状态.具体实现如下: zo ...

  4. RPC漏洞

    DCOM漏洞:利用这个漏洞攻击者只需发送特殊形式的清求到远程计算机上的135端口,轻则会造成拒绝服务攻击,严重的甚至可以让远程攻击者以本地管理员权限执行任何操作. 攻击过程:扫描也可用xscan+DC ...

  5. 原生Js实现拖拽(适用于pc和移动端)

    效果: HTML和CSS部分 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  6. js基础(闭包)

    1. "闭包就是跨作用域访问变量." [示例一] var name = 'wangxi' function user () { // var name = 'wangxi' fun ...

  7. 关于webApi使用session

    1.关于webApi使用session 在Global.asax中注册session添加以下代码 public override void Init() { //开启session this.Post ...

  8. Git Status 中文乱码解决

    现象: jb@H39:~/doc$ git statusOn branch masterYour branch is up-to-date with 'origin/master'. Untracke ...

  9. 写到 HTML 文档

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. python读取文件下的所有文档

    两类文档存储在两个路径下,假设每类文档有25个文档 def spamTest(): docList = [] classList = [] fullText = [] for i in range(1 ...