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

Q7:Reverse Integer

时间:2018-03-26 23:32:03      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:signed   whether   while   eal   res   lan   tps   note   blank   

7. Reverse Integer

官方的链接:7. Reverse Integer

Description :

Given a 32-bit signed integer, reverse digits of an integer.

Example1:


Input: 123

Output: 321


 Example2:


 Input: -123

Output: -321


 Example3:


 Input: 120

Output: 21


Note:

Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

问题描述

 反转32位的数字

思路

 上一次的模*10 + 这一次的模。中间判断是否有溢出

注意:last_mod * 10 + this_mod,而x /= 10

[github-here]

 1 public class Q7_ReverseInteger {
 2     public int reverse(int x) {
 3         int revResult = 0;
 4         while (0 != x){
 5             int newResult = revResult * 10 + x % 10;
 6             //judge whether it overflows
 7             if (newResult / 10 != revResult) {
 8                 return 0;
 9             }
10             revResult = newResult;
11             x /= 10;
12         }
13         return revResult;
14     }
15 }

 

Q7:Reverse Integer

标签:signed   whether   while   eal   res   lan   tps   note   blank   

原文地址:https://www.cnblogs.com/wpbxin/p/8654620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!