[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

解法一:

class Solution {
public:
    /**
     * @param n the integer to be reversed
     * @return the reversed integer
     */
    int reverseInteger(int n) {
        long long res = 0;
        while (n != 0) {
            res = 10 * res + n % 10;
            n /= 10;
        }
        return (res < INT_MIN || res > INT_MAX) ? 0 : res;
    }
};

解法二:

class Solution {
public:
    /**
     * @param n the integer to be reversed
     * @return the reversed integer
     */
    int reverseInteger(int n) {
        int res = 0;
        while (n != 0) {
            int t = res * 10 + n % 10;
            if (t / 10 != res) return 0;
            res = t;
            n /= 10;
        }
        return res;
    }
};
时间: 2024-10-26 18:54:44

[LintCode] Reverse Integer 翻转整数的相关文章

[LeetCode] 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 throu

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

lintcode 容易题:reverse integer 颠倒整数

题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接反转,越界处理好炒蛋 Java程序: public class Solution { /** * @param n the integer to be reversed * @return the reversed integer */ public int reverseInteger(int n

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 * 输入一个整数对其进行翻转

[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 反转整数

1 class Solution { 2 public: 3 int reverse(int x) { 4 int e,s,num,k=0; 5 num=x; 6 e=0; 7 if(x<0) 8 num=-1*x; 9 while( num!=0 ){ 10 s=num%10; 11 e=e*10+s; 12 num=num/10; 13 k++; 14 } 15 if(x<0) 16 return -e; 17 else 18 return e; 19 } 20 }; 题意: Exampl

7. 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 already thought throug

7. Reverse Integer 反转整数

[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: 负数.末尾有0均可用该方法处理 [思维问题]: [一句话思路]: 分离一位数.一边变长 另一边变短 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理