一天一道LeetCode系列

(一)题目

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

(二)解题

这题看上去很简单,动笔一挥之下,写出如下代码:

class Solution {
public:
    int reverse(int x) {
        bool flag = false;
        if(x<0) {
            x=-k;
            flag = true;
        }
        long result=0;
        while(x!=0)
        {
            result*=10;
            result+=x%10;
            x=x/10;
        }
        return flag==true ?-result:result;
    }
};

满心以为一步就能Accepted。没想到直接wrong answer!!

1534236469反过来9646324351超过了int的最大值,于是修改代码:

class Solution {
public:
    int reverse(int x) {
        long k = x;
        bool flag = false;
        if(k<0) {
            k=-k;
            flag = true;
        }
        long result=0;
        while(k!=0)
        {
            result*=10;
            result+=k%10;
            k=k/10;
        }
        if(result>2147483648||result<-2147483647) return 0;//判断越界没有
        else return flag==true ?(int)-result:(int)result;//结果判断正负输出
    }
};

这下能accepted了!果然越是看起来简单的题目越需要细节处理!

【一天一道LeetCode】#7. Reverse Integer的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. 【一天一道LeetCode】#12 Integer to Roman

    一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...

  3. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  4. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  5. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  6. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  7. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  8. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

  9. LeetCode 7. Reverse Integer (倒转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  10. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

随机推荐

  1. Spark:Spark 编程模型及快速入门

    http://blog.csdn.net/pipisorry/article/details/52366356 Spark编程模型 SparkContext类和SparkConf类 代码中初始化 我们 ...

  2. 为什么函数式编程可以没有while?

    以前想不通,今天在写代码时不知怎么的,偶然就发现了答案.. 比如说把某个字符串s中所有"00"及更长的'00'统统换为'0'.最后结果中不能包含'00'. 00001100--&g ...

  3. SRAM/DRAM,PROM/EPROM/EEPROM,NOR/NAND FLASH区别

                          SRAM/DRAM,PROM/EPROM/EEPROM,NOR/NAND FLASH区别 RAM / ROM 存储器 ROM和RAM指的都是半导体存储器,R ...

  4. Servlet - Listener、Filter、Decorator

    Servlet 标签 : Java与Web Listener-监听器 Listener为在Java Web中进行事件驱动编程提供了一整套事件类和监听器接口.Listener监听的事件源分为Servle ...

  5. activiti实战系列 activiti连线

    11:连线 11.1:流程图 注意:如果将流程图放置在和java类相同的路径,需要配置: 11.2:部署流程定义+启动流程实例 11.3:查询我的个人任务 11.4:完成任务 说明: 1)使用流程变量 ...

  6. bash shell while语法

    在编写脚本时,一定要注意空格 基本语法: while [ condition ] do command1 command2 command3 done condition为true时命令1到命令3将会 ...

  7. 4-sum问题

    给定一个整数数组,判断能否从中找出4个数a.b.c.d,使得他们的和为0,如果能,请找出所有满足和为0个4个数对. #define SIZE 10 void judgeAndPut(int* arr, ...

  8. Java基本语法-----java二维数组

    由于word里的样式在csdn上调太麻烦了,所以我再次贴图了,后面二维数组那里是文字的,大家将就看吧. 二维数组常见的操作: 1.遍历二维数组 2.对二维数组求和 class Demo { // 定义 ...

  9. iOS 10 推送全解析,注意事项

    本文旨在对 iOS 推送进行一个完整的剖析,如果你之前对推送一无所知,那么在你认真地阅读了全文后必将变成一个推送老手,你将会对其中的各种细节和原理有充分的理解.以下是 pikacode 使用 iOS ...

  10. DBoW2算法原理介绍

    本篇介绍DBoW2算法原理介绍,下篇介绍DBoW2的应用. DBow2算法 DBow2是一种高效的回环检测算法,DBOW2算法的全称为Bags of binary words for fast pla ...