Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: 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 already thought through this!

If the integer‘s last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

考虑边界条件,开始想先求出最大位,再倒推回去求res。后来看到可以在while里边倒推x,边增加res,更加简洁。int的最大值2的31次方-1,0x7fffffff;最小值为-的2的31次方,0x80000000;

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4
 5         const int max=2147483647;
 6         const int min=-2147483648;
 7         long long res=0;
 8         while(x!=0)
 9         {
10             res=res*10+x%10;
11             x=x/10;
12             if(res>max||res<min)
13             {
14                 return 0;
15             }
16         }
17
18         return res;
19     }
20 };
时间: 2024-08-06 20:06:44

Reverse Integer的相关文章

Reverse Integer - 反转一个int,溢出时返回0

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 若反转的数溢出,直接返回0 可以用计算结果来判断溢出,也可以用因数来判断 Java代码实现: 1 public class ReverseInteger { 2 public static int reverseInt(int x){ 3 if (x == 0) { 4 return

[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

水题 Reverse Integer

public class Solution { int reverse(int x) { long result = 0; while(x!=0){ result = result*10 + x%10 ; x/=10 ; } if(result>Integer.MAX_VALUE || result<Integer.MIN_VALUE ) return 0 ; else return (int)result ; } } https://leetcode.com/problems/reverse

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

[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.

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(7): Reverse Integer 源码实现 runtime: 8ms

题目 :Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 题目分析及部分代码解析: 1.需要考虑一位数,比如1,2,3等特殊情况,返回本身. 2.需要考虑0,返回0. 3.需要考虑如123000,45600等末尾有若干零的情况,正确结果应为321.654,不应该出现000321,00654等情况. 4.需要4字节int类型数据的取值范

LeetCode之“数学”:Reverse Integer &amp;&amp; Reverse Bits

1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: 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

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: 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 alr