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?
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
temp.f95:21:26: 21 | function isPalindrome(x) result(res) | 1 Error: Procedure βispalindromeβ at (1) has an explicit interface from a previous declaration temp.f95:22:21: 22 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:23:32: 23 | integer, intent(in) :: x | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:24:22: 24 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:20: 25 | integer :: y | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:27:13: 27 | y = x | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:24: 28 | do while (y > 0) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:29:46: 29 | if (mod(x, 10) /= mod(y, 10)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:30:29: 30 | res = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:31:22: 31 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:32:15: 32 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:33:22: 33 | x = x / 10 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:34:22: 34 | y = y / 10 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:11: 35 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:36:20: 36 | res = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:37:7: 37 | end function isPalindrome | 1 Error: Expecting END PROGRAM statement at (1)
! 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
At line 10 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7ba6eb8d7960 in ??? #1 0x7ba6eb8d84d9 in ??? #2 0x7ba6ebb2c17b in ??? #3 0x7ba6ebb25684 in ??? #4 0x7ba6ebb262aa in ??? #5 0x5acad1501209 in MAIN__ #6 0x5acad15013a4 in main
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:
original
to store the original integer value and reversed
initialized with 0 to store the reversed integer value.reversed
value by 10 and add the modulus of the integer (x) by 10.
b. Divide the integer (x) by 10, discarding the remainder.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.
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:
original
to store the original integer value and reversed
initialized with 0 to store the reversed integer value.reversed
value by 10 and add the modulus of the integer (x) by 10.
b. Divide the integer (x) by 10, discarding the remainder.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.