LeetCode Algorithm 07_Reverse Integer
2024-08-31 17:22:07
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Update (2014-11-10):
Test cases had been added to test the overflow behavior.
分析:本题理论上很简单,但对溢出的处理需要谨慎。这里本人用了unsigned long来保存reverse结果。
class Solution {
public:
int reverse(int x) {
bool isNegative = false;
if(x<){
x=-x;
isNegative = true;
}
unsigned long result = ;
while(x){
result = result* + x%;
x = x/;
}
if(result > INT_MAX) return ;
return (isNegative)? int(-result) : int(result) ; }
};
最新文章
- 最新WingIDE注册破解方法 【转】
- SQL数据库操作命令大全
- yum 安装 phpmyadmin
- Java 集合系列 17 TreeSet
- 【转】Linux写时拷贝技术(copy-on-write)
- B-Tree、B+Tree和B*Tree
- java设计模式--结构型模式--桥接模式
- Centos下搭建 nginx+uwsgi+python
- string转化为int方法
- Flex 各种校验
- 阿里云API网关(7)开发指南-API参考
- Linux启动顺序、运行级别及开机启动
- maven私库nexus2.3.0-04迁移升级到nexus-3.16.1-02(异机迁移备份)
- Java 7 for Absolute Beginners/Java 7基础教程--代码纠错
- [Umbraco] 自定义DataType中Data Editor Setting Type
- [转]SQL Server 2008 如何配置报表管理器
- Windows 2012桌面显示“我的电脑”
- android 获取配置文件 相对路径
- 中文名文件上传到linux服务器上以后文件名会乱码(openoffice)
- RESTful架构概念
热门文章
- Java技术——Java泛型详解(转)
- leetcode: Maximum Depth of Binary Tree
- HTTP服务器状态码定义
- javafx KeyCombination
- OpenCV —— 图像变换
- spring webSocket The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket
- 数据库事务及其EF中如何处理事务
- 【Mysql】将Excel表导入至Mysql的当中一张表
- using the easy connect naming method 简单连接測试
- Day4上午解题报告