Valid Palindrome
Valid Palindrome
zhuguorong 发表于2年前
Valid Palindrome
  • 发表于 2年前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

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.

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/*
 * 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 static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Solution.isPalindrome("race a car"));
	}
	public static boolean isPalindrome(String s) {
        if(s == null&&s.length() == 0)
        	return true;
        String slow = s.toLowerCase();
        char[] cc = slow.toCharArray();
        Stack<Character> stack = new Stack<>();
        Queue<Character> queue = new LinkedList<>();
        for(int i = 0 ; i < cc.length;i++)
        {
        	if((cc[i]>='0'&&cc[i]<='9')||(cc[i]>='a'&&cc[i]<='z'))
        	{
        		stack.push(cc[i]);
        		queue.add(cc[i]);
        	}
        	
        }
        boolean flag = true;
        while(!stack.isEmpty())
        {
        	if(stack.pop()!=queue.remove())
        	{
        		flag = false;
        		break;
        	}
        }
        return flag;
    }
}

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 5
码字总数 663
×
zhuguorong
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: