Lintcode27-Reverse 3-digit Integer

Reverse a 3-digit integer.

Example

Example 1:

Input: number = 123
Output: 321

Example 2:

Input: number = 900
Output: 9

Notice

You may assume the given number is larger or equal to 100 but smaller than 1000.

 

思路:

熟悉反转数字的方法,如何取每一位(取模 取余运算),取到每一位后如何变为反转数字(每一位*对应数量级)。

代码:

public int reverseInteger(int number) {
        int num1 = number % 10;
        int num2 = number / 10 % 10;
        int num3 = number / 100;
        int revNum = num1 * 100 + num2 * 10 + num3 * 1;
        return revNum;
    }

 

posted @ 2019-03-31 17:17  IreneZh  阅读(183)  评论(0编辑  收藏  举报