首页 > 其他 > 详细

LeetCode题目1 - Reverse Integer

时间:2015-11-24 21:59:19      阅读:235      评论:0      收藏:0      [点我收藏+]

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Discuss: 1.If the integer‘s last digit is 0, what should the output be? ie, cases such as 10, 100.
      2.Reversed integer overflow.

 

技术分享
public static int ReverseInteger(int num)
        {
            bool is_neg = num < 0;
            int newNum = Math.Abs(num);
            int result = 0;


            while (newNum > 0)
            {
                result = result * 10 + (newNum % 10);
                newNum /= 10;
            }

            if (is_neg)
                result *= -1;

            return result;
        }
View Code

 

LeetCode题目1 - Reverse Integer

原文:http://www.cnblogs.com/binyao/p/4992996.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!