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[ ]和目标值 ...
随机推荐
- SQL server 2008 安装报错 reporting services catalog database file existence
SQL server 2008 安装时报错 提示 reporting services catalog database file existence 查了一下,是因为原来装过Sql server 2 ...
- python入门13 集合set
set集合与数学中的集合同一个概念,是无序不重复元素组成的. #coding:utf-8 #/usr/bin/python """ 2018-11-10 dinghanh ...
- [转]查找问题的利器 - Git Bisect
转自:http://gitbook.liuhui998.com/5_4.html 假设你在项目的'2.6.18'版上面工作, 但是你当前的代码(master)崩溃(crash)了. 有时解决这种问题的 ...
- BZOJ4566:[HAOI2016]找相同字符(SAM)
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别 ...
- HDU 1083 Courses 【二分图完备匹配】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1083 Courses Time Limit: 20000/10000 MS (Java/Others) ...
- MyBatis的settings设置描述
settings 中的设置是非常关键的,它们会改变 MyBatis 的运行时行为.下表描述了设置中各项的意图.默认值等. 设置参数 描述 有效值 默认值 cacheEnabled 该配置影响的所有映射 ...
- 线段tree~讲解+例题
最近学习了线段树这一重要的数据结构,有些许感触.所以写一篇博客来解释一下线段树,既是对自己学习成果的检验,也希望可以给刚入门线段树的同学们一点点建议. 首先声明一点,本人是个蒟蒻,如果在博客中有什么不 ...
- 测试Storm的多源头锚定
过程, Spout 发送msgid 1-10 一级Bolt, msgid1的tuple做为基本组合tuple, 其他8个和一组合, 然后发送给二级Bolt, 同时单个msgid对应的tuple都ack ...
- JavaEE权限管理系统的搭建(八)--------角色的增删改
如下图所示,添加角色的同时,要给角色分配权限菜单,关于权限数的显示,我实现了两种方式,普通方式和Ztree方式, 普通方式展示树: 主要代码部分: /** * 进入角色添加页面 * @param mo ...
- 表达式过滤器 lowercase
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...