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[ ]和目标值 ...
随机推荐
- 通过一个实际例子理解Kubernetes里pod的自动scale - 水平自动伸缩
kubectl scale命令用于程序在负载加重或缩小时进行pod扩容或缩小,我们通过一些实际例子来观察scale命令到底能达到什么效果. 命令行创建一个deployment: kubectl run ...
- 【转载】#445 - Differences Between an Interface and an Abstract Class
An interface provides a list of members, without an implementation, that a class can choose to imple ...
- 为什么A经理的团队总是会陷入加班与救火之中
最近在看一本名为<稀缺>的书,作者从行为经济学的角度解释了穷人为什么会更穷,忙碌的人越来越没有时间,节食的人总是失败.由于缺乏闲余导致的带宽负担会进一步导致稀缺,由于总是优先处理紧急的事情 ...
- IOS UIImagePickerController(拍照或者读取相册)
UIImagePickerController ● 使用UIImagePickerController就可以进行拍照或者读取相册 ● 通过sourceType属性来决定拍照还是读取相册 ➢ UII ...
- public class Promise<T>: Thenable, CatchMixin
public class Promise<T>: Thenable, CatchMixin
- bzoj 1597 斜率DP
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5115 Solved: 1897[Submit] ...
- 【洛谷P3225】[HNOI2012]矿场搭建
矿场搭建 题目链接 根据题意,发生事故时会有一个挖煤点坍塌, 只有当这个点是割点,会对图的连通性产生影响, 我们首先Tarjan一遍找到所有割点,将原图除去这些割点后, 遍历一遍,找出所有连通块,分三 ...
- C#结构体和字节数组的转换函数
在通信过程中,一般我们都会操作到字节数组.特别是希望在不同语言编程进行操作的时候. 虽然C#提供了序列化的支持,不用字节数组也行.但操作字节数组肯定会碰到. 一般都会采用结构来表示字节数组.但结构 ...
- mysql获取汉字首字母函数
DELIMITER ;;CREATE FUNCTION `GET_FIRST_PINYIN_CHAR`(PARAM VARCHAR(255)) RETURNS VARCHAR(2) CHARSET u ...
- IPC进程间通信---消息队列
消息队列 消息队列:消息队列是一个存放在内核中的消息链表,每个消息队列由消息队列标识符标识.与管道不同的是消息队 列存放在内核中,只有在内核重启(即操作系统重启)或者显式地删除一个消息队列时,该消息队 ...