Leetcode Reverse Integer
2016-07-06 00:54
369 查看
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Difficulty: Easy
public class Solution {
public int reverse(int x) {
boolean isNeg = false;
if(x < 0) isNeg = true;
x = Math.abs(x);
double ans = 0;
while(x != 0){
ans = ans*10 + x%10;
x = x/10;
}
if(ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE)
return 0;
if(isNeg) return -(int)(ans);
return (int)(ans);
}
}
Example1: x = 123, return 321
Example2: x = -123, return -321
Difficulty: Easy
public class Solution {
public int reverse(int x) {
boolean isNeg = false;
if(x < 0) isNeg = true;
x = Math.abs(x);
double ans = 0;
while(x != 0){
ans = ans*10 + x%10;
x = x/10;
}
if(ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE)
return 0;
if(isNeg) return -(int)(ans);
return (int)(ans);
}
}
相关文章推荐
- iOS视频直播初窥:高仿<喵播APP>
- Leetcode ZigZag Conversion
- Windows 下安装Apache web服务器
- APP反编译基本流程
- Leetcode Nim Game
- Linux学习笔记四
- JMS基本概念之一
- Leetcode Longest Substring Without Repeating Characters
- .NET Core初体验 - 在Mac下运行第一个Web示例程序
- Leetcode Reverse Words in a String
- ELK集群故障处理
- 数据结构与算法(每天更新)
- Leetcode Add Two Numbers
- HDU 5289 Assignment(线段树)
- 一种c#深拷贝方式完胜java深拷贝(实现上的对比)
- CMake 介绍 & CMakeList.txt
- 老毛桃U盘启动盘制作工具V20140501完美贡献版
- 老毛桃U盘启动盘制作工具V20140501完美贡献版
- 稳定排序和不稳定排序
- 102. Binary Tree Level Order Traversal