14-Reverse Integer
思路:
先判定符号,整型范围[-2^32,2^32]
取余除10操作,依次进行,越界返回0
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
#define IMAX numeric_limits<int>::max()
#define IMIN numeric_limits<int>::min()
class Solution {
public:
int reverse(int x) {
int sign= x>0?1:-1;
if(x==IMIN)return 0;
else x = abs(x);
int res=0,count=0;
for(;x;x/=10)
{
if(count>9)return (sign==1)?IMAX:IMIN;
res = res*10;
if(count==8){ //倒数第二位向后看会不会越界,越界返回0
if(sign==1&&res>IMAX/10) return 0;
if(sign==-1&&res*sign<IMIN/10) return 0;
}
if(count==9){
if(sign==1&&(IMAX-res<=x%10))return 0;//最后一位向后看不会越界,越界返回0
if(sign==-1&&(res*sign-IMIN<=x%10)) return 0;
}
count++;
res=x%10+res;
}
return res*sign;
}
};
14-Reverse Integer的更多相关文章
- Python字符串倒序-7. Reverse Integer
今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- Reverse Integer 2015年6月23日
题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
随机推荐
- xmake v2.5.9 发布,改进 C++20 模块,并支持 Nim, Keil MDK 和 Unity Build
xmake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能 ...
- CF398A Cards | 贪心
题目链接 我怎么连这种题都做得那么艰难-- 可以发现一些结论,然后枚举'x'被分成几段就好了. 我真的越来越菜 #include<iostream> #include<cstdio& ...
- 『学了就忘』Linux基础命令 — 18、Linux命令的基本格式
目录 1.命令提示符说明 2.命令的基本格式 (1)举例ls命令 (2)说明ls -l命令的 输出内容 1.命令提示符说明 [root@localhost ~] # []:这是提示符的分隔符号,没有特 ...
- oracle 修改表空间名
1.登录使用sys用户登录 sqlplus sys/ as sysdba 2.修改表空间名字 SQL> alter tablespace 旧表空间名 rename to 新表空间名; 表空间已更 ...
- lumen、laravel问题汇总
框架报500 1.chmod 777 -R storage 将日志目录权限设置下. 2.修改fastcgi,将代码目录包含进去. fastcgi_param PHP_ADMIN_VALUE " ...
- Kali安装Parallels Tools过程记录
最近两天又参加了公司一年一度的网络安全劳动竞赛,之前用过的一个 Kali 忘记密码进不去了 -_- .重新安装了 Kali 2021.3a 之后发现 Parallels Tools 安装失败,记录了一 ...
- 攻防世界 WEB 高手进阶区 TokyoWesterns CTF shrine Writeup
攻防世界 WEB 高手进阶区 TokyoWesterns CTF shrine Writeup 题目介绍 题目考点 模板注入 Writeup 进入题目 import flask import os a ...
- idea断点调试
基本使用 1 show execution point (Alt+F10):跳转到断点所执行的地方,也就是说你在看代码的时候,点到其他地方,一点这个按钮,就到了程序执行到当前哪行的代码的地方. 2 s ...
- Vue 之 Mixins (混入)的使用
是什么 混入 (mixins): 是一种分发 Vue 组件中可复用功能的非常灵活的方式.混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被合并到组件本身,也就是说父组件调用混入 ...
- 运行级别和找回root密码
运行级别说明 0 :关机 1 :单用户 [类似安全模式,这个模式可以帮助找回root密码 2:多用户状态没有网络服务 3:多用户状态有网络服务 [使用] 4:系统未使用保留给用户 5:图形界面 6:系 ...