Java [leetcode 7] Reverse Integer
问题描述:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the
input is a 32-bit integer, then the reverse of 1000000003 overflows. How
should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
解题思路:
需要考虑数值为负的情况,需要考虑数值反过来后溢出的情况。
代码如下:
public class Solution {
public int reverse(int x) {
final String LowerValue = "2147483648";
final String UpperValue = "2147483647";
final int maxlength = 10;
String sOriginal = String.valueOf(x);
String sReverse = stringReverse(sOriginal);
String sfinal = new String();
int reversenum;
if (sReverse.charAt(sReverse.length() - 1) == '-') {
sfinal = sReverse.substring(0, sReverse.length() - 1);
if (sfinal.length() >= maxlength && sfinal.compareTo(LowerValue) > 0)
reversenum = 0;
else
reversenum = -Integer.valueOf(sfinal).intValue();
} else {
sfinal = sReverse;
if (sfinal.length() >= maxlength && sfinal.compareTo(UpperValue) > 0)
reversenum = 0;
else
reversenum = Integer.valueOf(sfinal).intValue();
}
return reversenum;
}
public String stringReverse(String s) {
StringBuilder stringBuilder = new StringBuilder(s);
stringBuilder.reverse();
return stringBuilder.toString();
}
}
Java [leetcode 7] Reverse Integer的更多相关文章
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- leetcode 7. Reverse Integer [java]
public int reverse(int x) { long res = 0; while (x != 0){ res = res* 10 + x % 10; x /= 10; } if(res ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
随机推荐
- uva 991
卡特兰数 最后不输出空行... #include <cstdio> #include <cstdlib> #include <cmath> #include &l ...
- substr_replace()函数:将手机号中间4位隐藏为*号
<?php $mobile = "15810320826"; echo substr_replace($mobile,'****',3 , 4); ?> substr_ ...
- sql查询数据库中所有表的记录条数,以及占用磁盘空间大小。
SELECT TableName = obj.name, TotalRows = prt.rows, [SpaceUsed(KB)] = SUM(alloc.used_pages)* FROM sys ...
- lintcode :Reverse Words in a String 翻转字符串
题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...
- 两个List合并,过滤重复记录
import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; import java.util.I ...
- activiti5.13 框架 数据库设计说明书
转载自:http://www.cnblogs.com/llzgzljl/p/3356108.html activiti5.13 框架 数据库设计说明书 1.结构设计 1.1. 逻辑结构设计 Ac ...
- js中鼠标滚轮事件详解
js中鼠标滚轮事件详解 (以下内容部分内容参考了http://adomas.org/javascript-mouse-wheel/ ) 之前js 仿Photoshop鼠标滚轮控制输入框取值中已使用 ...
- 利用 java.lang.Runtime.addShutdownHook() 钩子程序,保证java程序安全退出
以前在开发时只知道依靠数据库事务来保证程序关闭时数据的完整性. 但有些时候一个业务上要求的原子操作,不一定只包括数据库,比如外部接口或者消息队列.此时数据库事务就无能为力了. 这时我们可以依靠java ...
- windows/ubuntu 文件共享之 Samba 配置
很多时候需要在windows上和ubuntu 上共享文件,一直没怎么去找方法,得知Samba 可以实现在windows上访问linux的文件,这样一来要从windows文件放到linux中就方便了,听 ...
- python 字符串换行的三种方式
if __name__ == '__main__': #第一种: 三个单引号 print ''' aaaaaaaaaaaaaaaa bbbbbbbbbbbbbb''' #第二种: 三个 ...