leetcode 刷题:Valid Palindrome(回文字符串)
来源:互联网 发布:手机五线谱记谱软件 编辑:程序博客网 时间:2024/05/04 12:54
题目:
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.
思路:先将字符串中的字母跟数字按顺序提取出来放到一个新的字符串中,再用while循环比较新的字符串相对应位置的字符是否相等。若出现不相等的情况则返回false,循环完毕都没有返回FALSE,则是回文字符串。
注意:比较的时候是忽略大小写的。
public class Solution2 {
public boolean isPalindrome(String s) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i)) || Character.isLetter(s.charAt(i))){
System.out.println(s.charAt(i));
sb.append(s.charAt(i));
}
}
String str = sb.toString();
System.out.println(str);
int j = 0;
int k = str.length()-1;
boolean isPalindrome = true;
while(j<=k){
if(Character.toUpperCase(str.charAt(j)) == Character.toUpperCase(str.charAt(k))){
j++;
k--;
}else{
return false;
}
}
return isPalindrome;
}
public static void main(String[] args) {
Solution2 s2 = new Solution2();
System.out.println(s2.isPalindrome("`l;`` 1o1 ??;l`"));
}
}
结果输出true.
- leetcode 刷题:Valid Palindrome(回文字符串)
- LeetCode OJ 之 Valid Palindrome(回文字符串)
- (LeetCode)Valid Palindrome --- 判断回文字符串
- LeetCode Valid Palindrome(验证回文字符串)
- 【字符串回文】Valid Palindrome
- Valid Palindrome(回文字符串)
- 【LeetCode】- Valid Palindrome(正确的回文)
- 125. Valid Palindrome (回文字符串判断)
- leetcode 判断有效字符串是回文串Valid Palindrome
- <LeetCode>125.Valid Palindrome 验证回文字符串 Tag:string
- leetcode:Valid Palindrome 合法回文
- LeetCode - Valid Palindrome (判断“回文”)
- LeetCode (16) Valid Palindrome (回文)
- Valid Palindrome 判断回文字符串
- leetcode125---Valid Palindrome(回文字符串)
- Leetcode 字符串 Valid Palindrome
- LeetCode 之 Valid Palindrome(字符串)
- LeetCode 125 Valid Palindrome(有效回文)(*)
- poj 2570 Fiber Network (Floyd)
- 完美解决office无法正常卸载以及卸载后无法重装
- myeclipse扩大tomcat内存
- 不要放弃你的梦想
- 将QT开发的界面程序封装成DLL,在VC中成功调用
- leetcode 刷题:Valid Palindrome(回文字符串)
- 剑指offer——扑克牌的顺子
- gdb的使用
- C语言练习2
- wamp配置
- Android 多页面左右滑动
- 并发 生产者/消费者 读者/写者 哲学家进餐的理解
- Hive创建表时使用Unicode字符作为字段分隔符(hive unicode character as delimiter)
- 动态规划 UVA - 562 Dividing coins