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

LeetCode题目1 - Reverse Integer

时间:2015-11-24 22:01:04      阅读:112      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!