Java One-liner. Accepted. 21ms.


  • -3
    O
    public class Solution {
        public boolean isPalindrome(int x) {
            return x < 0 ? false : Integer.toString(x).equals(new StringBuilder(Integer.toString(x)).reverse().toString());
        }
    }

  • 0

    You can easily make it around 50 chars shorter (though it's a bit slower then).


  • 0
    O

    Of course. The two Integer.toString(x) calls could have been extracted into a String s (saving some 10 chars). The tenary operator could also be eliminated from the return statement (saving another 7 chars). It starts getting messier from that point if one still doesn't want to accumulate lines.


  • 0

    What I meant is still a one-liner and pretty clear, I think:

    public boolean isPalindrome(int x) {
        return (x + "").equals(new StringBuilder(x + "").reverse() + "");
    }
    

Log in to reply
 

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