Simple, accepted Java solution with O(n) time complexity


  • 0
    Z

    Extremely simple solution that just traverses the string front-to-back and back-to-front with two pointers, making comparisons along the way. Works with 100% of the test cases.

    public class Solution {
        public boolean isPalindrome(int x) {
            
            String str  = Integer.toString(x);
            
            for (int i = 0, j = str.length()-1; i < str.length() && j >= 0; i++,j--) {
                if (str.charAt(i) != str.charAt(j)) {
                    return false;
                }
            }
            
            return true;
        }
    }
    

Log in to reply
 

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