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 ...
随机推荐
- mount --bind 重启后失效的解决办法
vsftp不支持软链接,可以用mount来支持不同的目录结构 mount --bind /home/www/web/ROOT/img/upload /ftp/private/upload 重启后失效. ...
- ubuntu14.04和win7共享文件夹
环境:vmware12 问题:安装了vmware-tools,但是在/mnt/hgfs下面看不到共享的文件夹. 按照网上的一些经验和教程使用如下命令: mount -t vmhgfs .host:/ ...
- MAC OS X 常用通用快捷键
注:由于使用的是Windows键盘,习惯了Ctrl + c/v复制粘贴,所以修改了修饰键,Command(⌘)键和Control(^)键互换,以下的Ctrl键均为Command键,对应键盘上的实际左C ...
- C#文本选中及ContextMenuStrip菜单使用
'文本框选中显示'TextBox1.SelectAll()选择所有文本'textBox1.Text.Insert(start,strInsertText)指定位置添加文本1 Private Sub T ...
- 检索COM 类工厂中 CLSID 为 {} 的组件时失败
- css 自动换行 [英文、数字、中文]
white-space:normal;overflow: auto;table-layout:fixed; word-break: break-all;
- html与js传json值给php
//一段js代码 var data = {}, act = [], list = []; $('.set').find('input, textarea').each(function() { act ...
- lazyload
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- xmpp xml基本语义
基本语义 9.2.1 消息语义 <message/>节种类可被看作“推”机制,一个实体推信息给其它实体,与 EMAIL 系统中发生的通信类似.所有消息节应该拥有‘to’ 属性,指定有意的消 ...
- [Android Pro] 将你的安卓手机屏幕共享到PC或Mac上
有时候为了方便演示一个手机app,需要把手机屏幕显示到PC或Mac上.这里提供一个方法 — 使用Vysor达到此功能. Vysor的吸引力在于3个方面: 它适用于Windows.Linux或Mac. ...