Сначала позаботимся о следующих кейсах
- if X is negative ( not a palindrome )
- if X is less than ten ( always a palindrome )
- if X has 0 at its last digit and X is not 0 itself ( not a palindrome ) e.g. 10, 130 whose reverse will be 01, 031 respectively
Далее, следующая логика
- convert the number to a string
- take two pointers, at the start and end of the string
- if the digits at both pointers are different, it's not a palindrome
- we increment starting pointer and decrement the end pointer iteratively
- if the loop exits, then it was a palindrome
Это все, что нам нужно для решения задачи.
Time and space complexityTime complexityWe see a bit of improvement in run time. We are running logic only for positive numbers greater than 9. Also, in the loop, we are taking two steps instead of 1. However, asymptotically the running time complexity is still
O(len x).
Space complexityWe have a number as input, using a couple of more temporary variables, so space complexity is constant,
O(1).