java


  • 0
    P

    I have no idea about why my solution is so complex......(。_ 。|||)
    public class Solution {
    public boolean isPalindrome(String s) {
    if(s.length() == 0||s.length() == 1)
    {
    return true;
    }
    char[] chr1 = s.toCharArray();
    StringBuilder sb = new StringBuilder();
    for(int i = 0;i<chr1.length;i++)
    {
    if(Character.isAlphabetic(chr1[i])||Character.isDigit(chr1[i]))
    {
    sb.append(chr1[i]);
    }
    }
    String temp = sb.toString();
    char[] chr = temp.toCharArray();
    Stack<Character> stack = new Stack<Character>();
    for(int i = 0;i<chr.length/2;i++)
    {
    stack.push(chr[i]);
    }
    int begin = chr.length/2;
    if(chr.length%2 != 0)
    {
    begin++;
    }
    for(int i = begin;i<chr.length;i++)
    {
    char tempchar = stack.pop();
    if(tempchar != chr[i]&&tempchar != Character.toUpperCase(chr[i])&&tempchar!=Character.toLowerCase(chr[i]))
    {
    return false;
    }
    }
    return true;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.