Reverse Integer (JAVA)
public class Solution {
public int reverse(int x) {
StringBuffer sb = new StringBuffer(x+"").reverse();
if(sb.charAt(sb.length()-1)=='-')
{
sb=new StringBuffer(sb.substring(0,sb.length()-1));
}
int res=0;
try {
res= Integer.parseInt(sb.toString());
} catch (Exception e) {
return 0;
}
return x>0?res:-res;
}
}
队列
if(x==Integer.MIN_VALUE)
return 0;
Queue queue=new LinkedList();
int num=Math.abs(x);
int res=0;
while(num!=0)
{
queue.offer(num%10);
num/=10;
}
while(!queue.isEmpty())
{
if(res>(Integer.MAX_VALUE-num%10)/10)
return 0;
res=res*10+(int)queue.poll();
}
return x>0?res:-res;
Reverse Integer (JAVA)的更多相关文章
- 7. Reverse Integer java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 代码如下: pub ...
- 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第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode第[7]题(Java):Reverse Integer 标签:数学
题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- 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 ...
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
随机推荐
- Javascript进阶篇——浏览器对象—Location、Navigator、userAgent、screen对象
Location对象location用于获取或设置窗体的URL,并且可以用于解析URL.语法: location.[属性|方法] location对象属性图示: location 对象属性: loca ...
- CSS的优先级
样式的优先级: (内联样式表[嵌入式样式])>(内部样式表)>(外部样式表) 经过测试动手测试发现有个(唯一的)例外 情况:当引用外部样式在内部样式表(非嵌入式样式)的后面时,外部样式会覆 ...
- Javascript - IE8下parseInt()方法的取值异常
公司的测试小妹妹跑来对我说,下拉框第9项始终无法正确提交的时候,我还以为见鬼了. parseInt()会把'0'开头的数字以8进制来解析,当有大于7的数字时候就按10进制来解析. // p ...
- event和window.event
type:事件的类型,如onlick中的click:srcElement/target:事件源,就是发生事件的元素:button:声明被按下的鼠标键,整数,1代表左键,2代表右键,4代表中键,如果按下 ...
- php缩放gif和png格式透明背景变成黑色的解决方法
在对gif或png格式的图片进行缩放等操作时,原本透明背景的图片最后都变成黑色的,解决办法 $img = imagecreatetruecolor(, ); //2.上色 $color=imageco ...
- POJ2485 最小生成树
问题:POJ2485 本题求解生成树最大边的最小值 分析: 首先证明生成树最大边的最小值即最小生成树的最大边. 假设:生成树最大边的最小值比最小生成树的最大边更小. 不妨设C为G的一个最小生成树,e是 ...
- android的编译和运行过程深入分析
android的编译和运行过程深入分析 作者: 字体:[增加 减小] 类型:转载 首先来看一下使用Java语言编写的Android应用程序从源码到安装包的整个过程,此过程对了解android的编译和运 ...
- UFI命令格式里SCSI指令
有三种字长命令:6位.10位.12位,一般Windows下用12位. 在UFI 命令格式里SCSI指令用到如下: 指令代码 指令名称 说明 04h Format Unit 格式化存储单元 12h In ...
- SWFUpload批量上传插件
SWFUpload是一个批量上传插件,在HTML4.1里面,估计也只有Flash+javascript配合才能够做到了.先复制个重要的网址,这个应该是官方的文档了,相当齐全. http://leeon ...
- (?m)使用实例
示例sql: # User@Host: zjzc_app[zjzc_app] @ [10.22.18.164] Id: 6069153 # Query_time: 153.908486 Lock_ti ...