https://leetcode.com/problems/palindrome-number/

1. Revert half of the number

  1. //12 ms 5.9 MB
  2. class Solution {
  3. public:
  4. bool isPalindrome(int x) {
  5. if(x < 0)
  6. return false;
  7. if(x == 0)
  8. return true;
  9. //x > 0
  10. long long int curr = 0; //we have a test case 2147483647, over the limit of int
  11. long long int orig = x; //so apply long long int
  12. while(x != 0){
  13. curr = curr * 10 + (x % 10);
  14. x /= 10;
  15. }
  16. if(curr != orig)
  17. return false;
  18. else
  19. return true;
  20. }
  21. };

Time complexity: 9. Palindrome Number - 图1
Space complexity: O(1)