No.007 Reverse Integer
7. Reverse Integer
- Total Accepted: 153147
- Total Submissions: 644103
- Difficulty: Easy
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路:
本题思路很简单,有多种方法:要注意的就是判断反转之后的结果是否超出了int类型的范围。
- 第一种是直接对10取余和除,然后每加一位,就将原先的结果乘以10后加上余数即得从最低位到当前位反转之后的结果。然后将处理后的字符串转化为long类型,判断是否超出了范围,超出则输出0,没有则直接输出结果。
- 第二种是将数转化为String类型,判断index为0的位数是不是负号,若不是,则将整个字符串反转,若是,则将除了第0 位之后的字符串反转,然后将处理后的字符串转化为long类型,判断是否超出了范围,超出则输出0,没有则直接输出结果。
下面的程序只是第一种方法的代码:
public int reverse(int x) {
long res = 0 ;
while(x != 0){
res = res*10 + x%10 ;
x = x/10 ;
}
//判断是否超出了范围
if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE){
return 0 ;
}else{
return (int)res ;
}
}
No.007 Reverse Integer的更多相关文章
- LeetCode--No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【LeetCode】007. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [Leetcode]007. Reverse Integer
public class Solution { public int reverse(int x) { long rev=0; while(x!=0){ rev = rev*10+x%10; x=x/ ...
- 007 Reverse Integer 旋转整数
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example ...
- 007. Reverse Integer
题目链接:https://leetcode.com/problems/reverse-integer/description/ Given a 32-bit signed integer, rever ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
- Python字符串倒序-7. Reverse Integer
今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...
随机推荐
- 109. Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Android SDK的docs访问速度很慢(新)
#设置环境变量 名称:ANDROID_SDK_HOME 值:我的为-->E:\android\android-sdk #代码编译及运行 1.把下面的代码保存为:AndroidDocRepair. ...
- AP_AP系列 - 供应商管理(案例)
2014-07-03 Created By BaoXinjian
- POJ 1155 TELE 背包型树形DP 经典题
由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...
- NeHe OpenGL教程 第二十七课:影子
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- OpenGL 开始学习指南
近期需要做一个涌潮的预报与仿真模拟,为了使模型更具有真实感,且逼真,使用起来更灵活.感觉还是得从基础的OpenGL学习.鉴于Direct3D技术存在的众多不确定性,且评论不太好的原因,决定用OpenG ...
- apache 开启服务器包含(SSI)技术
SSI(server-side includes)能帮我们实现什么功能: SSI提供了一种对现有HTML文档增加动态内容的方法, 即 在html中加入动态内容 SSI是嵌入HTML页面中的指令,在页 ...
- [实变函数]2.3 开集 (open set), 闭集 (closed set), 完备集 (complete set)
1 $$\beex \bea E\mbox{ 是开集}&\lra E^o=E\\ &\lra \forall\ P_0\in E,\ \exists\ U( ...
- LPC1768之看门狗
- linux下安装easy_install的方法
python中的easy_install工具,类似于Php中的pear,或者Ruby中的gem,或者Perl中的cpan,那是相当的爽歪歪了如果想使用 如果想使用easy_install工具,可能需要 ...