码迷,mamicode.com
首页 > 其他好文 > 详细

7. Reverse Integer

时间:2015-04-30 07:33:24      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

注意溢出,溢出返回0

public class Solution {
  public int reverse(int x) {
    int sign = 1;
    if (x < 0) {
      sign = -1;
      x = -x;
    }
    long y = 0;
    while (x > 0) {
      y = y * 10 + x % 10;
      x = x / 10;
    }
    int ret = (int) y;
    if (y != ret) {
      return 0;
    }
    return ret * sign;
  }
}

7. Reverse Integer

标签:

原文地址:http://www.cnblogs.com/shini/p/4467917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!