[LC] 7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Solution 1: comparison to handle overflow
class Solution {
public int reverse(int x) {
int res = 0;
while (x != 0) {
int cur = res;
res = 10 * res + x % 10;
if (cur != res / 10) {
return 0;
}
x /= 10;
}
return res;
}
}
Solution 2: use Long to handle overflow
class Solution {
public int reverse(int x) {
long res = 0;
while (x != 0) {
res = 10 * res + x % 10;
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) {
return 0;
}
x /= 10;
}
return (int)res;
}
}
[LC] 7. Reverse Integer的更多相关文章
- Python字符串倒序-7. Reverse Integer
今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- Reverse Integer 2015年6月23日
题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
随机推荐
- javaweb03 javaservlet基础一
1.使用JavaEE版的eclipse开发动态的WEB工程(JavaWEB 项目)1).把开发选项切换到JavaEE2).可以在window -> Show View 中找到Package Ex ...
- ubuntu服务器上配置tomcat
前言 嗯,最近想在自己的腾讯云服务器上跑个项目玩玩,由于服务器是重装的系统,所以,只能自己手动装tomcat. 不过,tomcat是基于java的,必须又java环境tomcat才能够使用,因此首先要 ...
- [Java-基础]反射_Class对象_动态操作
动态性 动态语言 在程序运行时,可以改变程序结构或变量类型,典型的语言: Python,ruby,javascript 如: function test(){ var s = "var a= ...
- java查看简单GC日志
测试代码: public class GCtest { public static void main(String[] args) { for (int i = 0; i < 10000; i ...
- php list的用法
<?php $my_array = array("Dog","Cat","Horse"); list($a, $b, $c) = $m ...
- Centos7.6部署rsyslog+loganalyzer+mysql日志管理服务器
参考来自: the_script :https://yq.aliyun.com/articles/675198 名山.深处:https://www.cnblogs.com/skychenjiajun/ ...
- UML-如何使用层进行设计?
1.将代码组织映射为层和UML包 com.mycompany |_nextgen |_ui |_domain |_service |_util org.apache.log4j 2.使用对象设计应用 ...
- UML-领域模型-准则
1.是否使用工具维护模型? 在白板上画完草图后,整理到UML工具里去 2.模型中是否要包含“票据”? 不包含,因为,票据用于退货,而本次迭代不涉及退货所以不需要体现. 总结:概念一定在本次迭代需求内的 ...
- linux mysql备份数据库
$ mysqldump -u root -p 数据库名称 > beifen.sql 恢复 source beifen.sql
- Less(7)
1.先判断注入类型 (1)首先看到要求,要求传一个ID参数,并且要求是数字型的:?id=1 (2)再输入:?id=1 and 1=1 (3)输入:?id=1 and 1=2 因为(2)(3)没有变化, ...