Valid Palindrome

来源:互联网 发布:中学生编程竞赛 编辑:程序博客网 时间:2022/08/14 09:10
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 stubSystem.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 0
原创粉丝点击