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[ ]和目标值 ...
随机推荐
- ArcGIS10.1之crossdomain文件
大家都知道在10.1之前的版本在开发的时候需要使用跨域部署文件crossdomain.xml文件,在10.1中该文件不需要单独拷贝到IIS根目录或者是java版本的weboutput目录,在serve ...
- BZOJ1123:[POI2008]BLO(双连通分量)
Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...
- Zookeep启动异常:Error contacting service. It is probably not running.
异常提示: [root@hadoop bin]# ./zkServer.sh status JMX enabled by default Using config: /usr/local/zk/bin ...
- 原生ajax接收json字符串(简单介绍)
什么是json? JSON的全称是 Javascript Object Notation(javascript对象表示法),是基于javascript对象字面量,如果单从眼睛看,JSON里的数据是被保 ...
- 【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay
题目链接:https://www.luogu.org/problemnew/show/P3369 #include <cstdio> #include <algorithm> ...
- Android 滑动效果汇总
Android 滑动效果入门篇(一)—— ViewFlipper Android 滑动效果入门篇(二)—— Gallery Android 滑动效果基础篇(三)—— Gallery仿图像集浏览 And ...
- redis的数据结构与命令
以下部分文档,摘自51cto讲师:汤小洋 redis提供五种数据类型:string,hash,list,set及zset(sorted set). Redis数据就是以key value形式来存储的 ...
- C# String与StringBuilder (转载)
1.什么时候用String?什么时候用StringBuilder? 字符串一旦创建就不可修改大小,所以对字符串添加或删除操作比较频繁的话.那就不要用String而用StringBuilder. 例如: ...
- java流汇总以及使用实例
流一.基本概念 Java中对文件的操作是以流的方式进行的.流是Java内存中的一组有序数据序列.Java将数据从源(文件.内存.键盘.网络) 读入到内存中,形成了流,然后将这些流还可以写到另外的目的地 ...
- golang-Tag
Tag 理解 Golang中可以对struct定义Tag 例如: type TestTag struct{ UserName string `json:"name"` Age In ...