[关闭]
@zimenglan 2014-11-29T04:02:14.000000Z 字数 377 阅读 909

Reverse Integer

leetcode


Title

题目链接

  1. Reverse digits of an integer.
  2. Example1: x = 123, return 321
  3. Example2: x = -123, return -321

Code

  1. class Solution {
  2. public:
  3. int reverse(int x) {
  4. // may be overflow when inverse the input
  5. long long base = 1L;
  6. int Max_Int = (int)((base << 31) - 1);
  7. int Min_Int = (int)(0 - (base << 31));
  8. long long res = 0;
  9. while(x != 0) {
  10. res = res * 10 + (x % 10);
  11. x = x / 10;
  12. }
  13. if(res > Max_Int || res < Min_Int) {
  14. return 0;
  15. }
  16. return (int)res;
  17. }
  18. };
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注