LeetCode——Reverse Integer(逆置一个整数)
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).
简单的分析逆置一个整数比较简单,代码如下:
- public class Solution {
- public int reverse(int x) {
- int result = 0;
- while(x != 0){
- result = result*10+x%10;
- x = x/10;
- }
- return result;
- }
- }
上面的代码对于类似10100这样的数逆置之后就变为了101,也算可以接受,但是没有考虑溢出的问题。
在考虑溢出这个问题上,
我们可以先把原数x的符号位记录下来,在java中x的符号位可以获取为 int sign=x>>31,当sign为-1表示为负数,否则为正。
然后按照上面的方法求逆置的数result,再判断result的符号位是否和原数x相同,若相同则没有溢出;否则溢出。
实现的代码如下:
- public int reverse2(int x) {
- int result = 0;
- int sign = x>>31;
- while (x != 0) {
- result = result * 10 + x % 10;
- x = x / 10;
- }
- // System.out.println(sign+", "+(result>>31));
- if(sign!=(result>>31)){
- System.out.println("overflow..");
- System.exit(1);
- }
- return result;
- }
以下是我用于测试的完整代码:
- public class ReverseInt {
- public static void main(String[] args) {
- ReverseInt r = new ReverseInt();
- System.out.println(r.reverse2(123));
- System.out.println(r.reverse2(1230));
- System.out.println(r.reverse2(-123));
- System.out.println(r.reverse2(1000000003));
- }
- public int reverse(int x) {
- int result = 0;
- while (x != 0) {
- result = result * 10 + x % 10;
- x = x / 10;
- }
- return result;
- }
- public int reverse2(int x) {
- int result = 0;
- int sign = x>>31;
- while (x != 0) {
- result = result * 10 + x % 10;
- x = x / 10;
- }
- // System.out.println(sign+", "+(result>>31));
- if(sign!=(result>>31)){
- System.out.println("overflow..");
- System.exit(1);
- }
- return result;
- }
- }
LeetCode——Reverse Integer(逆置一个整数)的更多相关文章
- leetcode:Integer to Roman(整数转化为罗马数字)
Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...
- [leetcode]273. Integer to English Words 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- LeetCode Reverse Bits 反置位值
题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. class Solution { public: uint32_t r ...
- C++ leetcode::Reverse Integer
第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...
- [LeetCode] 273. Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- Leetcode: Reverse Integer 正确的思路下-要考虑代码简化
题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have ...
随机推荐
- Linux之图形化shell------dialog
转自:Linux dialog详解(图形化shell) | 运维生存时间 对话 UNIX: 使用 shell 脚本创建好的图形应用程序---http://www.ibm.com/developerwo ...
- python程序打包成.exe----pyinstaller工具
1. 环境 windows 2. 安装 准备文件:PyWin32 or pypiwin32 运行如下安装命令: pip install pyinstaller==3.0 不要使用3.2版本,编译完成 ...
- poj1177
题意:在平面直角坐标系内给出一些与坐标轴平行的矩形,将这些矩形覆盖的区域求并集,然后问这个区域的周长是多少.(边与边重合的地方不计入周长) 分析:线段树.曾经做过类似的求矩形覆盖的总面积的题.这道题同 ...
- find参数exec、管道符|、xargs的区别
1.这三个命令都可以将前面的输出做为后面的输入. 2.他们对于前面的输出,处理方式不同. find . -name "*.sh" -exec cat {} \; find . -n ...
- log4net使用
平时项目里一直都有在使用log4net作为日志记录模块,当时一直都没有去理解log4net的配置文件信息.今天抽出了一点时间来看了看配置文件信息. log4net配置文件信息: <log4net ...
- 【python】入门学习(六)
type() #检查变量或值得数据类型 >>> type(5) <class 'int'> 序列:包括字符串.元组和列表.序列都可以用索引.切片.len()(计算元素个数 ...
- 加载ComboBox控件
/// <summary> /// 加载公司 /// </summary> /// <param name="cbbCompany">Combo ...
- solrconfig.xml和schema.xml说明
1. solrconfig.xml solrconfig.xml配置文件主要定义了SOLR的一些处理规则,包括索引数据的存放位置,更新,删除,查询的一些规则配置. 1.1. datadir节点 ...
- Mac 下 gradle 路径
/Users/yourname/.gradle/wrapper/dists cmd:cd ~/.gradle/wrapper/dists/
- js 闭包原理理解
问题?什么是js(JavaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 很显然 ...