leetcode — reverse-integer
/**
* Source : https://oj.leetcode.com/problems/reverse-integer/
*
* Created by lverpeng on 2017/7/4.
*
* 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 ReverseInteger {
/**
* 翻转一个int类型的数,几种特殊情况考虑
* 1. 末尾是零的处理方式:去掉
* 2. 溢出:一种方式注解返回0,一种是当做无符号int输出
*
* 从右至左取出每一位数,然后组成新的数
* 取余,整除的方法
*
* @param num
* @return
*/
public int reverse (int num) {
int temp = 0;
int result = 0;
while (num != 0) {
// 判断溢出
if (result > Integer.MAX_VALUE / 10 || result < Integer.MIN_VALUE / 10) {
// 溢出返回0
return 0;
}
// 最后一位数
temp = num % 10;
result = result * 10 + temp;
// 去除最后一位
num = num / 10;
}
return result;
}
/**
* 溢出不返回0,返回翻转后的数,使用long表示
* @param num
* @return
*/
public long reverse1 (int num) {
int temp = 0;
long result = 0;
while (num != 0) {
// 最后一位数
temp = num % 10;
result = result * 10 + temp;
// 去除最后一位
num = num / 10;
}
return result;
}
public static void main(String[] args) {
ReverseInteger reverseInteger = new ReverseInteger();
System.out.println(reverseInteger.reverse(123) + " === " + "321");
System.out.println(reverseInteger.reverse(-123) + " === " + "-321");
System.out.println(reverseInteger.reverse(-100) + " === " + "-1");
System.out.println(reverseInteger.reverse(1002) + " === " + "2001");
System.out.println(reverseInteger.reverse(123) + " === " + "321");
System.out.println(reverseInteger.reverse(1463847412) + " === " + "2147483641");
System.out.println(reverseInteger.reverse(2147447412) + " === " + "2147447412");
System.out.println(reverseInteger.reverse(2147447412) + " === " + "2147447412");
// overfow
System.out.println(reverseInteger.reverse(1000000003) + " === " + "0");
System.out.println(reverseInteger.reverse(2147483647) + " === " + "0");
System.out.println(reverseInteger.reverse(-2147483648) + " === " + "0");
System.out.println("");
System.out.println("-------------");
System.out.println("");
//
System.out.println(reverseInteger.reverse1(123) + " === " + "321");
System.out.println(reverseInteger.reverse1(-123) + " === " + "-321");
System.out.println(reverseInteger.reverse1(-100) + " === " + "-1");
System.out.println(reverseInteger.reverse1(1002) + " === " + "2001");
System.out.println(reverseInteger.reverse1(123) + " === " + "321");
//
System.out.println(reverseInteger.reverse1(1463847412) + " === " + "2147483641");
System.out.println(reverseInteger.reverse1(2147447412) + " === " + "2147447412");
System.out.println(reverseInteger.reverse1(2147447412) + " === " + "2147447412");
// overflow
System.out.println(reverseInteger.reverse1(1000000003) + " === " + "3000000001");
System.out.println(reverseInteger.reverse1(2147483647) + " === " + "7463847412");
System.out.println(reverseInteger.reverse1(-2147483648) + " === " + "-8463847412");
}
}
leetcode — reverse-integer的更多相关文章
- 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 ...
- C++ leetcode::Reverse Integer
第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- LeetCode——Reverse Integer(逆置一个整数)
问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321 Ha ...
- Leetcode: Reverse Integer 正确的思路下-要考虑代码简化
题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
- LeetCode——Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- [Leetcode]Reverse Integer
核心思想:原数对10取余数赋值给新数后降一位,再把新数升一位加上下一次原数取余值,直到原数降为0. 解法如下: int reverse(int x) { bool minus = false; ) ...
- leetcode reverse integer&&Palindrome Number
public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...
随机推荐
- Linux驱动之定时器在按键去抖中的应用
机械按键在按下的过程中会出现抖动的情况,如下图,这样就会导致本来按下一次按键的过程会出现多次中断,导致判断出错.在按键驱动程序中我们可以这么做: 在按键驱动程序中我们可以这么做来取消按键抖动的影响:当 ...
- Linux驱动之异步通知的应用
前面的按键驱动方式都是应用程序通过主动查询的方式获得按键值的: 1.查询方式 2.中断方式 3.poll机制 下面介绍第四种按键驱动的方式 4.异步通知:它可以做到应用程序不用随时去查询按键的状态,而 ...
- osg探究补充:osg数据加载原理(插件机制简介)
前言 我们接着昨天的继续,昨天主要是讲解了DatabasePager类中的特定的成员变量以及run函数的第一部分,对所要请求加载的数据按照是否是网络数据进行分类加载模式.今天我们就看看数据是怎们加载到 ...
- 如何学好游戏3D引擎编程
注:本文是网上看到的一篇文章,感觉写的很好,因此收藏了下来 <如何学好游戏3D引擎编程>此篇文章献给那些为了游戏编程不怕困难的热血青年,它的神秘要我永远不间断的去挑战自我,超越自我,这样才 ...
- dpkg: error: dpkg status database is locked by another process
First run: lsof /var/lib/dpkg/lock Then make sure that process isn't running: ps cax | grep PID If i ...
- 解决win10系统无法安装.NET Framework 3.5
方法1:“我的电脑”,单击右键选择“管理” 在打开的“计算机管理”窗口中依路径“服务和应用程序—服务”打开,在列表中找到“Windows Update”并双击打开. 启动类型:自动. 服务状态下面点击 ...
- 网站日志流量分析采集(LuaJIT系统环境部署-node03,相关jar包自己手动上传)
注:/usr/local/src 是源码包路径,可以自己更改 服务器中安装依赖 yum -y install gcc perl pcre-devel openssl openssl-devel 上传 ...
- s2 Docker环境的快速搭建方法
常规linux下安装 centos7 下配置docker源并安装 cat >/etc/yum.repos.d/docker.repo< [dockerrepo] name=Docker R ...
- canvas 旋转
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 查看服务器tcp连接及服务器并发
一.查看哪些IP连接本机netstat -an二.查看TCP连接数1)统计80端口连接数netstat -nat|grep -i "80"|wc -l 2)统计httpd协议连接数 ...