Java Solution without REAL -A-N-Y- extra space


  • 0
    S

    This solution, although ugly does not use any extra space.
    The maximum integer value is 2147483647, which has the length of 10.
    I am manually looping backwards from 10 to 1 to find the length of the number and then test it if it is a palindrome

    public class Solution {
         public static boolean isPalindrome(int x) {
            if (x < 0 ) return false;
            try {
                if (x / 1000000000 == 0) throw new Exception("");
                if (x / 1000000000 != x % 10 ) {
                    return false;
                }
                if (x / 100000000 % 10 != x / 10 % 10 ) {
                    return false;
                }
                if (x / 10000000 % 10 != x / 100 % 10 ) {
                    return false;
                }
                if (x / 1000000 % 10 != x / 1000 % 10 ) {
                    return false;
                }
                if (x / 100000 % 10 != x / 10000 % 10 ) {
                    return false;
                }
                if (x / 10000 % 10 != x / 100000 % 10 ) {
                    return false;
                }
            } catch (Exception ex) {
                try {
                    if (x / 100000000 == 0) throw new Exception("");
    
                    if (x / 100000000 != x % 10 ) {
                        return false;
                    }
                    if (x / 10000000 % 10 != x / 10 % 10 ) {
                        return false;
                    }
                    if (x / 1000000 % 10 != x / 100 % 10 ) {
                        return false;
                    }
                    if (x / 100000 % 10 != x / 1000 % 10 ) {
                        return false;
                    }
                    if (x / 10000 % 10 != x / 10000 % 10 ) {
                        return false;
                    }
                    if (x / 1000 % 10 != x / 100000 % 10 ) {
                        return false;
                    }
                } catch (Exception ex2) {
                    try {
                        if (x / 10000000 == 0) throw new Exception("");
                        if (x / 10000000 != x % 10 ) {
                            return false;
                        }
                        if (x / 1000000 % 10 != x / 10 % 10 ) {
                            return false;
                        }
                        if (x / 100000 % 10 != x / 100 % 10 ) {
                            return false;
                        }
                        if (x / 10000 % 10 != x / 1000 % 10 ) {
                            return false;
                        }
                        if (x / 1000 % 10 != x / 10000 % 10 ) {
                            return false;
                        }
                        if (x / 100 % 10 != x / 100000 % 10 ) {
                            return false;
                        }
                    } catch (Exception ex3) {
                        try {
                            if (x / 1000000 == 0) throw new Exception("");
                            if (x / 1000000 != x % 10 ) {
                                return false;
                            }
                            if (x / 100000 % 10 != x / 10 % 10 ) {
                                return false;
                            }
                            if (x / 10000 % 10 != x / 100 % 10 ) {
                                return false;
                            }
                            if (x / 1000 % 10 != x / 1000 % 10 ) {
                                return false;
                            }
                            if (x / 100 % 10 != x / 10000 % 10 ) {
                                return false;
                            }
                            if (x / 10 % 10 != x / 100000 % 10 ) {
                                return false;
                            }
                        } catch (Exception ex4) {
                            try {
                                if (x / 100000 == 0) throw new Exception("");
                                if (x / 100000 != x % 10 ) {
                                    return false;
                                }
                                if (x / 10000 % 10 != x / 10 % 10 ) {
                                    return false;
                                }
                                if (x / 1000 % 10 != x / 100 % 10 ) {
                                    return false;
                                }
                                if (x / 100 % 10 != x / 1000 % 10 ) {
                                    return false;
                                }
                                if (x / 10 % 10 != x / 10000 % 10 ) {
                                    return false;
                                }
                            } catch (Exception ex5) {
                                try {
                                    if (x / 10000 == 0) throw new Exception("");
                                    if (x / 10000 != x % 10 ) {
                                        return false;
                                    }
                                    if (x / 1000 % 10 != x / 10 % 10 ) {
                                        return false;
                                    }
                                    if (x / 100 % 10 != x / 100 % 10 ) {
                                        return false;
                                    }
                                    if (x / 10 % 10 != x / 1000 % 10 ) {
                                        return false;
                                    }
                                } catch (Exception ex6) {
                                    try {
                                        if (x / 1000 == 0) throw new Exception("");
                                        if (x / 1000 != x % 10 ) {
                                            return false;
                                        }
                                        if (x / 100 % 10 != x / 10 % 10 ) {
                                            return false;
                                        }
                                        if (x / 10 % 10 != x / 100 % 10 ) {
                                            return false;
                                        }
                                    } catch (Exception ex7) {
                                        try {
                                            if (x / 100 == 0) throw new Exception("");
                                            if (x / 100 != x % 10 ) {
                                                return false;
                                            }
                                            if (x / 10 % 10 != x / 10 % 10 ) {
                                                return false;
                                            }
                                        } catch (Exception ex8) {                                       
                                                if (x < 10) return true;
                                                if (x / 10 != x % 10 ) {
                                                    return false;
                                                }                                       
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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