[关闭]
@chawuciren 2018-11-15T15:14:57.000000Z 字数 493 阅读 572

9. Palindrome Number

leetcode


Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

  1. bool isPalindrome(int x) {
  2. int n=reverse(x);
  3. if(n>=0&&n==x)
  4. return true;
  5. else
  6. return false;
  7. }
  8. int reverse(int x) {
  9. if(x>=2147483647||x<=-2147483647){
  10. return 0;
  11. }
  12. int result=0;
  13. int a[10]={0};
  14. int k=0;
  15. int c=0;
  16. int n=x;
  17. while(n!=0){
  18. result=n%10;
  19. a[k]=result;
  20. n/=10;
  21. k+=1;
  22. }
  23. int b=k-1;
  24. result=0;
  25. for(int i=k;i>0;i--){
  26. for(int j=0;j<b;j++){
  27. a[c]*=10;
  28. }
  29. result+=a[c];
  30. c+=1;
  31. b-=1;
  32. }
  33. return result;
  34. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注