LeetCode:7. Reverse Integer(Easy)
题目要求:将给出的整数进行逆序输出

注意:整数的最大范围-2147483648~2147483647,当翻转后的数超出范围后返回0
思路:对给出的整数除以10,取余和取整;然后对取整部分继续取余和取整,同时将前一次取余的部分乘10再加上该次取余...直至余数为零
public class Solution {
public static void main(String[] args) {
Solution sol = new Solution();
// System.out.println(t.reverse(-123));
// System.out.println(t.reverse(1000));
// System.out.println(t.reverse(123));
System.out.println(sol.reverse(1324565656));
}
public int reverse(int x) {
// 单独处理MIN_VALUE,因为最大范围和最小范围绝对值差1
if (x == Integer.MIN_VALUE)
return 0;
int num = Math.abs(x);
int result = 0;
while (num != 0) {
// 判断是否超出MAX_VALUE
if (result > (Integer.MAX_VALUE - num % 10) / 10)
return 0;
result = result * 10 + num % 10;
num = num / 10;
}
// 最后判断正负号
return x > 0 ? result : -result;
}
}
LeetCode:7. Reverse Integer(Easy)的更多相关文章
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- leetcode 1.回文数-(easy)
2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
随机推荐
- Android(java)学习笔记35:如何改变Spinner系统自带的字体和颜色
1. 首先我们要知道Spinner系统自带字体和颜色本质: 原生的Spring 控件是无法更改字体和颜色的... 从下面的代码可以看出...红色的标注显示使用的是Android默认的布局.. Spin ...
- Centos6.5(Linux)安装Nginx
1.安装nginx依赖的库pcre 下载地址:http://sourceforge.net/projects/pcre/ 2.解压pcre zip解压方式:unzip ...
- ACM-ICPC (10/14)
动态规划的四个姿势 动态规划要学好,姿势一定要骚,在实战的时候,你将你的转移方程按照以下四种姿势搞一发后,一定会是耳目一新,引来萌妹子的注意~~~哈哈!!! 言归正传了!!! 之所以写动态规划优化,是 ...
- 计算次数,POJ(1207)
题目链接:http://poj.org/problem?id=1207 #include <stdio.h> #include <algorithm> using namesp ...
- Ubuntu不支持rpm安装软件解决方法
Ubuntu不支持rpm安装软件解决方法 以前经常使用的是RedHat Linux,习惯使用rpm方法安装软件.最近发现Ubuntu系统居然不支持rpm方法安装软件,提示信息如下: root@root ...
- SP348 EXPEDI - Expedition
嘟嘟嘟 水贪心. 当经过一个加油站的时候,记下这个加油站能加的油,然后没油的时候从经过的加油站中选择加油最多的加. #include<cstdio> #include<iostrea ...
- activeMQ消息队列的使用
ActiveMQ解决问题: 1.解决服务之间的耦合 2.增加系统并发处理量. 它使用的是标准生产者和消费者模型.有两种数据结构:Queue/Topic 1.Queue队列,生产者生产一个消息,只能由 ...
- PSROIAlign的代码实现
https://github.com/afantideng/R-FCN-PSROIAlign
- 关于eclipse中引入项目报错或者没有JRE System Library问题(jre报错)或者jre1.7(8)改为jre1.8(7)等问题
解决方法: 右键项目工程-->>properties->>java bulid path -->>>libraries -->>add libra ...
- MVC学习四:Razor视图语法
@{ Layout = null; } <hr /> <!DOCTYPE html> @this.GetType().Assembly.Location.ToString() ...