leetcode--07 --Reverse Integer(逆转整数)
* 原题 * Reverse digits of an integer. * Example1: x = 123, return 321 * Example2: x = -123, return -321 * * 题目大意 * 输入一个整数对其进行翻转 * *
1 package com.hust0407;
2
3 import java.util.Scanner;
4
5 /**
6 * Created by huststl on 2018/4/7 10:34
7 * 输入一个整数对其进行翻转
8 */
9 public class Main04 {
10 //通过求余数求商法进行操作
11 public static void main(String[] args) {
12 Scanner scan = new Scanner(System.in);
13 while (scan.hasNext()){
14 int num = scan.nextInt();
15 System.out.println(reverse(num));
16 }
17 }
18
19 private static int reverse(int num) {
20 long tmp = num;
21 //防止结果溢出
22 long result = 0;
23 //余数求商法
24 while (tmp!=0){
25 result = result * 10 + tmp %10;
26 tmp = tmp/10;
27 }
28 //溢出判断
29 if(result<Integer.MIN_VALUE || result >Integer.MAX_VALUE){
30 result = 0;
31 }
32
33 return (int)result;
34 }
35 }
千里路我只陪你一程,从此艳阳风雪我不问!