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
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的更多相关文章
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
- C# 写 LeetCode easy #7 Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
- Leetcode练习题 7. Reverse Integer
7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...
- 【LeetCode】【Python题解】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- 【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...
- 【一天一道LeetCode】#7. Reverse Integer
一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- 【LeetCode】007. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- leetcode:7. Reverse Integer
这题简单,也花了我好长时间,我自己写的code比较麻烦,也没啥技巧:按正负性分类执行,先转化成字符串,用stringbuilder进行旋转,如果超出范围了就用try catch public int ...
随机推荐
- maven war项目完整配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 给MS的意见
2017-02-27 WPF的中文注释文档翻译得很烂.太多了,列举不过来. 这个是 System.Threading.Tasks.Task.Exception: 获取导致 System.Aggrega ...
- RS485转USB插电脑上通讯不上
在确定没有其他问题时,基本可以确定是干扰问题,换个24V电源试试,不要用原来的线 485接口确定,好坏通过两个相反对接,发送信息,两边一致,就可以
- mariadb笔记
MariaDB or MySQL DDL:数据定义语言 DML:数据操纵语言 skip_name_resolve = on :跳过域名解析: MariaDB [(none)]> s ...
- Spring Boot Actuator [监控与管理]
1. 如何添加 2. actuator 的原生端点(API) 2.1 应用类配置 2.1.1 http://localhost:8080/actuator/conditions 2.1.2 http: ...
- IntelliJ IDEA2018.3 最新破解方法
IntelliJ IDEA2018.3 最新破解方法 输入 http://idea.java.sx/ 即可,亲测可用.如果资金允许还是希望大家能支持正版,尊重原创 ------------- ...
- 深入理解CADisplayLink和NSTimer
一.什么是CADisplayLink 简单地说,它就是一个定时器,每隔几毫秒刷新一次屏幕. CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器.我们在应用中创建一 ...
- html入门1
1.HTML超文本标记语言,(Hypertext Markup Language),通过一段内容定义为标题,段落或者图像等,从而让该内容具有结构以及含义 2.W3C:万维网联盟(World Wide ...
- Delphi下的WinSock编程
一.定址 要通过Winsock建立通信,必须了解如何利用指定的协议为工作站定址.Winsock 2引入了几个新的.与协议无关的函数,它们可和任何一个地址家族一起使用:但是大多数情况下,各 ...
- 谷歌排名影响因素最新研究(SEM RUSH版)
谷歌排名影响因素,关于这块的研究在国外有很多,一全老师(www.yiquanseo.com)以前也专门翻译整合过两篇,分别是Backlinko的<谷歌排名影响因素权威报告(研究了数百万谷歌网站得 ...