一天一道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. 基于Nginx服务器和iOS9的HTTPS安全通信

    简介 在网络通信中,使用抓包软件可以对网络请求进行分析,并进行重放攻击,重放攻击的解决方案一般是使用一个变化的参数,例如RSA加密的时间戳,但考虑到网络传输时延,时间戳需要有一定的误差容限,这样仍然不 ...

  2. 安卓6.0新特性在Fragment申请运行时权限

    今天在Fragment申请权限时代码如下: public void getContacts(){ int flag = ActivityCompat.checkSelfPermission(getAc ...

  3. SSH网上商城---用户激活

    在前面的博客中,小编主要结合SSH网上商城这个项目,简单的介绍了如何实现邮件发送的这个功能,邮件发送了,接下来就是激活了,为什么呢?现在大多网站都要通过对账号进行激活,然后才能注册成功,这是防止恶性注 ...

  4. 指令汇B新闻客户端开发(六) 浅谈屏幕适配解决方案

    屏幕适配的问题,我相信很多大牛的经验远比我丰富,在此就简单的分享一下我所做的的屏幕适配方案,当然我说的是安卓方面的啦,嘿嘿,屏幕适配我们一般用1280*720的屏幕作为我们的主流开发屏,当然现在And ...

  5. Android项目开发填坑记-9patchPng报错

    如果阅读体验不佳,请使用–> Github版 背景 之前写了一篇文章Android必知必会–NinePatch图片制作详细介绍了Android 9Patch图片的制作和一些Demo展示,这次说明 ...

  6. AndroidStudio如何快速制作.so

    之前写过一篇Eclipse制作.so的文章,http://blog.csdn.net/baiyuliang2013/article/details/44306921使用的是GNUstep模拟Linux ...

  7. Linux:alias永久生效

    alias(中文称为"别名")允许使用更加简短的名称来重新定义 Linux 中的 Shell 命令,从而简化命令行的输入. 如果经常与 CLI 打交道,那么使用 alias 不仅会 ...

  8. 使用VideoView实现简单视频播放器

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/39471397 VideoView内部封装好了Mediaplayer.Android框架 ...

  9. Tensorflow使用Cmake在Windows下生成VisualStudio工程并编译

    传送门: https://github.com/tensorflow/tensorflow/tree/r0.12/tensorflow/contrib/cmake http://www.udpwork ...

  10. MySQL 如何使用索引 较为详细的分析和例子

    在数据库表中,使用索引可以大大提高查询速度. 假如我们创建了一个 testIndex 表: CREATE TABLE testIndex(i_testID INT NOT NULL,vc_Name V ...