Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

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) ; }
};

最新文章

  1. 最新WingIDE注册破解方法 【转】
  2. SQL数据库操作命令大全
  3. yum 安装 phpmyadmin
  4. Java 集合系列 17 TreeSet
  5. 【转】Linux写时拷贝技术(copy-on-write)
  6. B-Tree、B+Tree和B*Tree
  7. java设计模式--结构型模式--桥接模式
  8. Centos下搭建 nginx+uwsgi+python
  9. string转化为int方法
  10. Flex 各种校验
  11. 阿里云API网关(7)开发指南-API参考
  12. Linux启动顺序、运行级别及开机启动
  13. maven私库nexus2.3.0-04迁移升级到nexus-3.16.1-02(异机迁移备份)
  14. Java 7 for Absolute Beginners/Java 7基础教程--代码纠错
  15. [Umbraco] 自定义DataType中Data Editor Setting Type
  16. [转]SQL Server 2008 如何配置报表管理器
  17. Windows 2012桌面显示“我的电脑”
  18. android 获取配置文件 相对路径
  19. 中文名文件上传到linux服务器上以后文件名会乱码(openoffice)
  20. RESTful架构概念

热门文章

  1. Java技术——Java泛型详解(转)
  2. leetcode: Maximum Depth of Binary Tree
  3. HTTP服务器状态码定义
  4. javafx KeyCombination
  5. OpenCV —— 图像变换
  6. spring webSocket The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket
  7. 数据库事务及其EF中如何处理事务
  8. 【Mysql】将Excel表导入至Mysql的当中一张表
  9. using the easy connect naming method 简单连接測试
  10. Day4上午解题报告