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 }

原文地址:https://www.cnblogs.com/huststl/p/8732555.html

时间: 2024-10-09 21:28:50

leetcode--07 --Reverse Integer(逆转整数)的相关文章

[leetcode]7. Reverse Integer反转整数

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 题意: 给定一个10进制整数,翻转它. Solution1: directly do the simulation. Two tricky parts to be hand

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign

LeetCode:Reverse Integer - 翻转数字

1.题目名称 Reverse Integer(翻转数字) 2.题目地址 https://leetcode.com/problems/reverse-integer/ 3.题目内容 英文:Reverse digits of an integer. 中文:翻转一个正整数的各位,形成一个新数字 例如:x = 123, return 321:x = -123, return -321 4.一个有瑕疵的方法(不能AC) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方

LeetCode 007 Reverse Integer

[题目] Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 [题意] 反转int型整数,输出的也是int型的整数 [思路] 如要考虑两种特殊情况: 1. 类似100这样的整数翻转之后为1 2. 翻转之后的值溢出该如何处理, 本题的测试用例中似乎没有给出溢出的情况 在实际面试时需要跟面试官明确这种情况的处理方法. 基于这点事实,本题规定如果超出正边界返回INT_MA

Leetcode 数 Reverse Integer

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Reverse Integer Total Accepted: 17472 Total Submissions: 43938 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

LeetCode 7 Reverse Integer(反转数字)

题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 其实这道题看起来非常简单,要实现也是几行代码的事.但是有个小问题容易被忽略,就是边界问题.什么意思呢?如果我们输入的整数超出了int的表达范围,这个问题要怎么解决呢? 用比int更大的数据类型存储我们转

[LeetCode][JavaScript]Reverse Integer

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

[LintCode] Reverse Integer 翻转整数

Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). Have you met this question in a real interview? Example Given x = 123, return 321 Given x = -123, return -321 LeetCode上的原题,请参见我之前的博客Reverse Integer.