[Math]Reverse Integer

Total Accepted: 111287 Total Submissions: 474471 Difficulty: Easy

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.

(E) String to Integer (atoi)

class Solution {
public:
    int reverse(int x) {
        int sign = 1;
        if(x<0) {
            sign=-1;
            x=-x;
        }
        long long int res = 0;
        while(x){
            res = res*10 + x%10;
            x/=10;
        }
        res *= sign;
        res = res>INT_MAX || res<INT_MIN ? 0:res;
        return res;
    }
};

Next challenges: (E) Factorial Trailing Zeroes (M) Strobogrammatic Number II (H) Best Meeting Point

时间: 2024-11-11 22:52:41

[Math]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

LeetCode: Reverse Integer 解题报告

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. SOLUTION 1: 注意越界后返回0.先用long来计算,然后,把越界的处理掉. public class Solution { public int reverse(int x) { long ret = 0; while (x !

7. Reverse Integer 反转int

7. Reverse Integer 问题: 反转int,当有负号时需要保留负号. 解决思路: 1.先将int类型转换为string,按照之前写过的string类型做好反转,再转为int类型. 2.不做类型转换.先将负数转换为正数进行统一处理,然后int类型数每次%10得到的余即依次为个.十.百-位上的数字. 自己写的1:(java) 这里是根据思路1来做的,发现在string类型转为int类型时容易抛出异常,无法转换.因此只能采用其他方法. public class Solution { pu

leetcode_26题——Reverse Integer(int型的表示范围)

Reverse Integer Total Accepted: 73207 Total Submissions: 281775My Submissions Question Solution Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Hide Tags Math Have you met this questi

LeetCode之Easy篇 ——(7)Reverse Integer

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 Note:Assume we are dealing with an environment which could only hold

LeetCode-Algorithms #007 Reverse Integer, Database #182 Duplicate Emails

LeetCode-Algorithms #007 Reverse Integer 给定一个32位整数, 将其各位反转并返回, 如果结果超出取值范围就返回0 1 class Solution { 2 public int reverse(int x) { 3 //对原数取绝对值 4 int y = Math.abs(x); 5 //将原数转换为字符串 6 String s1 = Integer.toString(y); 7 //将字符串转换为字符数组 8 char[] arr = s1.toCha

水题 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) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方