leetcode--07 --Reverse Integer(逆转整数)

Posted huster-田浪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于leetcode--07 --Reverse Integer(逆转整数)的主要内容,如果未能解决你的问题,请参考以下文章

Reverse Words in a String

Reverse Words in a String--not finished yet

Reverse Words in a string

151. Reverse Words in a String

186. Reverse Words in a String II

Reverse Words in a String leetcode