题目:

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 integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

这个题目是一个easy的题目,但是细节要注意,第一点要注意记得处理负数的情况,

第二点就是记得看Note,Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

就是超过int的范围了就需要返回0。这一点非常重要,因为你要判断的是可能反转后会溢出,那么需要注意的就是

需要用一个long long来存储答案ans。判断使用的语句就是:

if(ans>INT_MAX||ans<INT_MIN)
            return 0;

啦啦啦!

代码如下:

 class Solution {
public:
int reverse(int x) {
long long ans=;
bool isLittle=false;
if(x<)
{
x=-x;
isLittle=true;
}
while(x>)
{
int num=x%;
ans=ans*+num;
x=x/;
}
if(ans>INT_MAX||ans<INT_MIN)
return ;
if(isLittle)
return -ans;
else return ans;
}
};

leetcode题解 7.Reverse Integer的更多相关文章

  1. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  2. C# 写 LeetCode easy #7 Reverse Integer

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

  3. Leetcode练习题 7. Reverse Integer

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  4. 【LeetCode】【Python题解】Reverse Integer

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

  5. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  6. 【一天一道LeetCode】#7. Reverse Integer

    一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...

  7. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

  8. 【LeetCode】007. Reverse Integer

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

  9. leetcode:7. Reverse Integer

    这题简单,也花了我好长时间,我自己写的code比较麻烦,也没啥技巧:按正负性分类执行,先转化成字符串,用stringbuilder进行旋转,如果超出范围了就用try catch public int ...

随机推荐

  1. 《BUG创造队》第一次作业:团队亮相

    项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 第五次实验 团队名称 BUG创造队 作业学习目标 通过本次项目了解并熟悉团队开发的过程 关于我们 -我们的队名:BUG创造队 ...

  2. istio路由配置

    istio路由配置   istio的代理配置参考文档: 中文文档: https://istio.io/zh/docs/reference/config/istio.networking.v1alpha ...

  3. C语言博客作业04——数组

    1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量 1.2.1学习体会 这几周学习了数组,数组分为三大块:一维数组.二维数组和字符数组.数组相对于之前普通变量的好处就是可以储存数值,方便数据 ...

  4. Linux getopt/getopts解析命令行参数教程

    一.说明 shell中获取参数可以直接使用$1.$2等形式来获取,但这种方式有明显的限制:每个参数的位置是固定的.比如如果在设计上$1是ip地址$2是端口,那在执行时就必须第一个参数是ip第二个参数是 ...

  5. DMA 内存存取原理

    DMA直接内存存取原理 DMADMA直接内存存取原理是指外部设备不通过CPU而直接与系统内存交换数据的接口技术. 要把外设的数据读入内存或把内存的数据传送到外设,一般都要通过CPU控制完成,如CPU程 ...

  6. rebar3自动编译

    功能:修改完代码可以自动编译加载到VM中 必须安装的软件: Linux: inotify  链接https://github.com/rvoicilas/inotify-tools/wiki 配置: ...

  7. java切割音频文件

    工具: 一个jar包即可:jave-1.0.2.jar 可以切割wav格式的音频文件 完整工程目录 就一个jar包,一个main类 代码: package com.zit; import java.i ...

  8. ES6 中 Promise

    在说Promise之前我们先简单说一下什么是同步异步? 同步(Sync):所谓同步,就是发出一个功能调用时,在没有得到结果之前,该调用就不返回或继续执行后续操作. 异步(Async):异步与同步相对, ...

  9. C++ MySQL编程

    MySQL编程需要包含<mysql.h>头文件.该文件一般在MySQL安装目录下的include文件夹下. 包含头文件还不够,还需要包含“libmysql.lib”库,一般在lib文件夹下 ...

  10. POST 调用 301 Moved Permanently 问题

    如上测试了2种,第一个post访问接口https能正常返回,第二个post访问接口http时301 Moved Permanently出现永久重定向问题,经检查发现服务器nginx对80端口做了重定向 ...