Given an integer x, return true if x is a palindrome__, and false otherwise.

Example 1:

Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Constraints:

  • -231 <= x <= 231 - 1

Follow up: Could you solve it without converting the integer to a string?


Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer :: x
    logical :: isPalindrome

    ! Example 1
    x = 121
    print '(L1)', isPalindrome(x)

    ! Example 2
    x = -121
    print '(L1)', isPalindrome(x)

    ! Example 3
    x = 10
    print '(L1)', isPalindrome(x)

contains

    function isPalindrome(x) result(res)
        implicit none
        integer, intent(in) :: x
        logical :: res
        integer :: y

        y = x
        do while (y > 0)
            if (mod(x, 10) /= mod(y, 10)) then
                res = .false.
                return
            end if
            x = x / 10
            y = y / 10
        end do
        res = .true.
    end function isPalindrome

end program main
βœ… Compiled
❌ Executed
❌ Correct
! This is a comment in Fortran

PROGRAM palindrome

IMPLICIT NONE

INTEGER :: x, reversed_x, remainder

! Read the input from the command line
READ(*,*) x

! Initialize the reversed_x variable
reversed_x = 0

! While the input number is greater than 0
DO WHILE (x > 0)

! Get the last digit of the number
remainder = MOD(x, 10)

! Add the last digit to the reversed number
reversed_x = reversed_x * 10 + remainder

! Divide the input number by 10
x = x / 10

END DO

! If the input number is equal to the reversed number, then it is a palindrome
IF (x == reversed_x) THEN

PRINT *, "True"

ELSE

PRINT *, "False"

END IF

END PROGRAM palindrome
🌐 Data from online sources
def is_palindrome(x: int) -> bool:
    if x < 0:
        return False
    original, reversed = x, 0
    while x > 0:
        reversed = reversed * 10 + x % 10
        x //= 10
    return original == reversed
Here is the algorithm:
  1. If the integer is negative, immediately return false, as negative numbers cannot be palindromes.
  2. Create a variable named original to store the original integer value and reversed initialized with 0 to store the reversed integer value.
  3. Iterate through the integer while it's greater than zero and, in each iteration, perform the following operations: a. Multiply the reversed value by 10 and add the modulus of the integer (x) by 10. b. Divide the integer (x) by 10, discarding the remainder.
  4. Compare original and reversed integers; if they are equal, return true; otherwise, return false.

This approach requires O(log10(n)) time complexity as we are dividing the input by 10 in each iteration.

🌐 Data from online sources
bool isPalindrome(int x) {
    if (x < 0) return false;
    int original = x, reversed = 0;
    while (x > 0) {
        reversed = reversed * 10 + x % 10;
        x /= 10;
    }
    return original == reversed;
}
Here is the algorithm:
  1. If the integer is negative, immediately return false, as negative numbers cannot be palindromes.
  2. Create a variable named original to store the original integer value and reversed initialized with 0 to store the reversed integer value.
  3. Iterate through the integer while it's greater than zero and, in each iteration, perform the following operations: a. Multiply the reversed value by 10 and add the modulus of the integer (x) by 10. b. Divide the integer (x) by 10, discarding the remainder.
  4. Compare original and reversed integers; if they are equal, return true; otherwise, return false.

This approach requires O(log10(n)) time complexity as we are dividing the input by 10 in each iteration.