Why my java code is wrong


  • 0
    W

    public class Solution {
    public boolean isPalindrome(String s) {
    int start=0;
    int end=s.length()-1;
    while(start<end){
    char a=s.charAt(start);
    char b=s.charAt(end);
    if(a<'a'||a>'z'&&a<'A'||a>'Z'&&a<'0'||a>'9'){
    start++;
    }else if(b<'a'||b>'z'&&b<'A'||b>'Z'&&b<'0'||b>'9'){
    end--;
    }else if(a!=b&&Math.abs(a-b)!=32){
    return false;
    }else{
    start++;
    end--;
    }
    }
    return true;
    }
    }


  • 0

    Who do you think is going to bother reading such unformatted code?


  • 1
    E

    should not use else /else if in whle loop. Instead, use the following in the while-loop
    while((a<'a'||a>'z'&&a<'A'||a>'Z'&&a<'0'||a>'9' )&& start < end){ start++; }
    while((b<'a'||b>'z'&&b<'A'||b>'Z'&&b<'0'||b>'9') && start < end){ end--; }
    if(Character.toLowerCase(s.charAt(start)) != Character.toLowerCase(s.charAt(end))) return false;
    start++; end--;


Log in to reply
 

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