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?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

数字逆转。

可转成字符数组。再处理。可是这样要考虑的情况比較多,并且使用了新的空间,效率不高。

能够通过求模运算获得依次低位,再把原乘以10加上新取得的数。

	public static int reverse(int x) {
int ret = 0;
while (x != 0) {
ret = ret * 10 + x % 10;
x /= 10;
}
return ret;
}

比方:输入-192,计算步骤例如以下,经过了三轮循环:

ret x

---------------

0 -192

-2 -192

-2 -19

---------------

-2 -19

-29 -19

-29 -1

---------------

-29 -1

-291 -1

-291 0

---------------

-291

最新文章

  1. 要想提高PHP的编程效率,你必须知道的要点
  2. ASP.NET Core 中文文档 第三章 原理(14)服务器
  3. 详解C#中的反射
  4. Quartz.net 定时调度CronTrigger时间配置格式说明
  5. [转]Hibernate时间总结
  6. python成长之路【第三篇】:函数
  7. SQLServer2008R2 mdf文件还原数据库
  8. shell之任务控制
  9. MyBatis学习总结_02_使用MyBatis对表执行CRUD操作
  10. 校友信息管理&SNS互动平台之前言、目录及说明
  11. Android 实时文件夹
  12. SharePoint 2010 应用url参数过滤列表视图数据(应用get办法过滤列表数据)
  13. 将vue的项目打包后通过百度的BAE发布到网上的流程
  14. Hive快捷查询:不启用Mapreduce job启用Fetch task三种方式介绍
  15. springboot整理
  16. js group by
  17. ELK & ElasticSearch 5.1 基础概念及配置文件详解【转】
  18. R语言比较运算符和逻辑运算符
  19. Samba远程代码执行漏洞(CVE-2017-7494)复现
  20. SparkStreaming任务保持运行,定时任务监控进程,保证不挂掉

热门文章

  1. (转)自定义UITabBar
  2. Java总结输入流输出流
  3. Leetcode 410.分割数组的最大值
  4. Centos6.5搭建git远程仓库
  5. document.execCommand
  6. 查询UNDO使用情况
  7. cf468C Hack it!
  8. spring security 登录、权限管理配置
  9. MongoDB GridFS(命令行+php操作)
  10. 漫话最小割 part1