题目:
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.

翻译:
反转一个整形数的数值。
你是否考虑了这些?
在写代码之前,这里有一些好问题需要被提出来讨论一下。如果你已经考虑到了这些问题,在面试中会是一个加分项。
如果整数的某位是0,那输出结果应该是怎样的?例如,像10,100这种用例。
你注意到反转后的整数可能导致溢出吗?假定输入是一个32位的整数,然后1000000003的反转后的结果会导致溢出。你怎样处理这些溢出?
考虑到这个问题,我们假定你的程序在反转溢出时返回0。

分析:
这道题目看似简单,实则非常容易出错,通过率也较低。原因在于结果溢出的处理上(根据题目的补充说明,溢出返回0),做题时,需要考虑以下几种特殊情况:
1. 在做反转的时候,我把输入的数字统一转化为正数处理。这个时候可能产生第一次溢出。
2. 当输入为类似100时,检查输出是否正确。
3. 在反转的过程中,由于可能产生溢出,我将中间结果采用long保存。
4. 将long转化为int时,需要根据输入的符号,再次判断是否溢出。

代码:

public class Solution {public int reverse(int x) {boolean flag=false;if(x<0){flag=true;if(-(long)x>Integer.MAX_VALUE){return 0;}x=-x;}long result=0;while(true){result=result*10+x%10;if(x<10){break;}x/=10;}if(flag){if(-result<Integer.MIN_VALUE){return 0;}return -(int)result;}if(result>Integer.MAX_VALUE){return 0;}return (int)result;}
}

Leet Code OJ 7. Reverse Integer [Difficulty: Easy]相关推荐

  1. Leet Code OJ 344. Reverse String [Difficulty: Easy]

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  2. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  3. Leet Code OJ 1. Two Sum [Difficulty: Easy]

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  4. Leet Code OJ 112. Path Sum [Difficulty: Easy]

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  5. Leet Code OJ 202. Happy Number [Difficulty: Easy]

    题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...

  6. Leet Code OJ 169. Majority Element [Difficulty: Easy]

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  7. Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]

    题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...

  8. Leet Code OJ 258. Add Digits [Difficulty: Easy]

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  9. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

最新文章

  1. PC问题-VMware Workstation出现“文件锁定失败”
  2. phalcon: 解决php7/phalcon3.2以上版本,不支持oracle数据库的方法
  3. 成功解决TabError: inconsistent use of tabs and spaces in indentation
  4. 北斗导航 | 卫星导航基础知识(卫星导航时间系统)
  5. 我是如何自学 Python 的,分享一下经验
  6. GAN 生成对抗网络论文阅读路线图
  7. web测试常用的用例及知识
  8. 零起点英语_【德国零起点】05—变元音字母
  9. 数据库实验3 数据库的单表查询
  10. 关于IOS屏幕的旋转问题
  11. Translatium for Mac(多语言在线翻译工具)
  12. 获取本地文件所需配置
  13. Atitit 防止迟到与防止打卡打不上解决方案 attilax总结
  14. 小波神经网络模型的建立,小波神经网络模型matlab
  15. 炫炫炫的十六进制编辑器
  16. python 制作抽奖箱_海安当地横幅制作值得推荐,抽奖箱制作-漫谈
  17. 第七章:实现、测试。黑盒测试、白盒测试、单元测试、集成测试
  18. 云顶之弈服务器维护多长时间,英雄联盟3.17更新维护时间介绍 云顶之弈什么时候可以玩_18183云顶之弈专区...
  19. 堪萨斯州立大学计算机专业,全美顶尖大学:堪萨斯州立大学
  20. 天正菜单栏不见了怎么显示出来_天正CAD中菜单栏不见了如何调出来?

热门文章

  1. [luogu4128][shoi2006]有色图
  2. (十一)boost库之多线程间通信
  3. Linux 进程(一) 进程概念和进程状态(僵尸进程、孤儿进程、守护进程)
  4. python 获取指定目录下的图片文件
  5. MyBatis(二)MyBatis基本流程源码分析
  6. shell编程之正则表达式
  7. 【今晚7点】:圆桌PI回归 继续聊聊开源的故事
  8. Instagram视频上传延迟优化
  9. Twitch如何实现转码器比FFmepg性能提升65%?(上)
  10. FFmpeg源代码:avcodec_receive_frame