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 ...
随机推荐
- android -------- 沉浸式状态栏和沉浸式导航栏(ImmersionBar)
android 4.4以上沉浸式状态栏和沉浸式导航栏管理,包括状态栏字体颜色,适用于Activity.Fragment.DialogFragment.Dialog,并且适配刘海屏,适配软键盘弹出等问题 ...
- 弹筐里同一个按钮判断是从哪里点击过来的form
点击弹框按钮 <form action="javascript:;" method="post"> <button type= ...
- 毕业设计《项目管理》总结06之ajax的初步使用经验
1.ajax页面时不能实现下载功能,因为后台下载功能返回的是一个流,而ajax得到后台的数据只能是字符串或字符,所以实现的方法可以: 例如:用js生成一个form,用这个form提交参数,并返回“流” ...
- Github 搭建 Hexo 纯静态化个人博客平台
以前一直想搭建一个属于自己的博客平台,有余种种原因一直未能实现,最近闲来无事,参照网上的教程,搭建了属于自己的博客.自己的博客网站,样式自由,不需要受限于各大平台. 本篇为从零开始的基础篇,本篇所包含 ...
- js 回调
function getTittleLableData(callback) { $.ajax({ type : 'POST', url : ctx + '/feeInf/getDataValue', ...
- 电脑小白和ta的小白电脑——Git的使用
简单介绍Git的安装和基本指令,不要抱太大希望QAQ 看完这篇博客,最多学会如何向远程库上传和从远程库拉取项目,复杂功能要 做中学! (一)Git的安装 1.下载 (1)官网下载地址: https:/ ...
- Navicat for Mysql导入mysql数据库脚本文件
1.鼠标右键点击,然后选中运行sql文件,执行,然后选中编码方式为Utf8,即可. 2.可能会出现一系列的问题,参照着报错,进行mysql配置文件的修改.
- shell脚本的多线程
shell脚本的多线程 #!/bin/bash ###这是个多线程脚本!!!! ..} do { .$i >/dev/null ];then echo "192.168.2.$i 存活 ...
- MinHook 分析01 (x86的jmp+offset类型hook)
MinHook的原理就在于重写目标函数.在这次分析的X86模式中,32位相对JMP覆盖了整个地址空间.因为在相对地址计算中溢出的位被忽略,所以在X86模式中,函数的地址是容易掌控的. 直接来进入正题. ...
- C#异常:未将对象引用设置到对象的实例。
异常:未将对象引用设置到对象的实例. 一般是定义的变量或者数组等,没有赋初始值. 赋初始值后问题解决.