码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode——Valid Palindrome

时间:2015-06-19 22:59:13      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

Description:

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.

public class Solution {
    public boolean isPalindrome(String s) {
        StringBuilder str = new StringBuilder();
        for(int i=0; i<s.length(); i++) {
            if(s.charAt(i)>=‘a‘&&s.charAt(i)<=‘z‘ || s.charAt(i)>=‘A‘&&s.charAt(i)<=‘Z‘
                || s.charAt(i)>=‘0‘&&s.charAt(i)<=‘9‘) {
                str.append(s.charAt(i));
            }
        }
        str = new StringBuilder(str.toString().toLowerCase());
        int flag = 1;
        for(int i=0; i<str.length()/2; i++) {
            if(str.charAt(i) != str.charAt(str.length()-i-1)) {
                flag = 0;
                break;
            }
            
        }
        if(flag == 1)
            return true;
        else 
            return false;
        
        
    }
}

 

LeetCode——Valid Palindrome

标签:

原文地址:http://www.cnblogs.com/wxisme/p/4589880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!