Valid Palindrome(easy)
来源:互联网 发布:淘宝双11红包怎么领 编辑:程序博客网 时间:2024/05/15 11:50
【题目】
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.
【题意】
判断一个字符串是否是回文字符串,只考虑字符数字,不考虑大小写并且空串是有效的回文。
【分析】
判读一个字符串是否是回文,一种方法可以将字符串倒置然后和原字符串进行比较。这里采用另一种方法,通过从前后两个方向来比较判断是否是回文
【实现】
public class Solution { public boolean isValid(char c){ if(c >= 'a' && c <= 'z') return true; if(c >= '0' && c <= '9') return true; return false; } public boolean isPalindrome(String s) { // Start typing your Java solution below // DO NOT write main() function if(s.equals("")) return true; s = s.toLowerCase(); int len = s.length(); int left = 0, right = len - 1; while(left < right){ while(!isValid(s.charAt(left))){ left++; if(left >= right) return true; } while(!isValid(s.charAt(right))){ right--; if(left >= right) return true; } if(s.charAt(left) != s.charAt(right)) return false; left++; right--; } return true;}}
0 0
- Valid Palindrome(easy)
- Leetcode[Easy] - Valid Palindrome
- 【Easy】125. Valid Palindrome
- [Leetcode 125, easy] Valid Palindrome
- <LeetCode><Easy> 125 Valid Palindrome
- 125. Valid Palindrome [easy] (Python)
- Easy 125题 Valid Palindrome
- leetcode 125 Valid Palindrome(难易度:Easy)
- Easy-题目60:125. Valid Palindrome
- Leetcode 125. Valid Palindrome (Easy) (cpp)
- Leetcode OJ 125 Valid Palindrome [Easy]
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
- LeetCode(125) Valid Palindrome
- 125. Valid Palindrome(回文)
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- Valid Palindrome
- 你的指尖 有改变世界的力量
- 数据结构之查找
- iOS开发-UI中懒加载的使用方法
- 各种排序算法的分析及java实现
- 图解HTTPS
- Valid Palindrome(easy)
- tcpdump抓包
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
- (考研)数据结构及算法
- H5网站如何检测移动设备横竖屏并旋转?
- UVa 10123 - No Tipping
- android:ListView下拉刷新上拉加载更多(PullToRefresh框架抽取)
- Unable to locate an executable at “/usr/bin/java/bin/java” (-1)
- jsp 总结