leetCode(62)-Reverse Integer
题目:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
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.
Update (2014-11-10):
Test cases had been added to test the overflow behavior.
思路:
- 题意:反转整数
- 注意两点:1.负数的情况2.溢出返回0(绝对值大于Integer.MAX_VALUE)
代码:
public class Solution {
public int reverse(int x) {
boolean flag = true;
if(x < 0){
flag = false;
x = x*-1;
}
long res = 0;
while(x > 0){
res = res *10 + x%10;
x = x / 10;
}
if(res > Integer.MAX_VALUE){
return 0;
}
if(flag){
return (int)res;
}else{
return (int)res * -1;
}
}
}
leetCode(62)-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 ...
- 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 ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
随机推荐
- Windows安装和使用fftw
FFTW是一个比较快的.非常出名的一个DFT的开源库. 本文探索安装和配置FFTW,用Visual Studio 2008来使用fftw. 第一步:下载最新的fftw库 这一步很简单,只要在googl ...
- 【shell点滴】参数变量
参数变量故名思议就是用来操作输入参数的变量,知道用户输入了哪些参数,才可以进行相应的处理. 参数变量 作用 $1,$2- 取第几个参数的意思 $* 取出所有的参数,解析参数的分割符环境变量 IFS 来 ...
- C++三目运算符的增强
<p>// 在C语言中表达式的结果放在寄存器中 // 在C语言中,表达式的返回值是变量的值 // 在C++中,表达式返回的是变量的本身</p><pre name=&quo ...
- scala学习笔记1(表达式)
<pre name="code" class="plain">//Scala中的 main 函数需要存在于 object 对象中,我们需要一个obj ...
- Linux中printk()实例
新建hello.c #include <linux/kernel.h> #include <linux/module.h> int init_module(void) { pr ...
- SpriteBuilder中返回的对象类型不正确的原因
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 最近在码代码的时候,发现一个问题,特此写出来和大家分享,希望遇到 ...
- Collections类解析
最常用的排序: 需要实现Comparable接口 1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它 ...
- C语言实现4种常用排序
实在没事搞,反正面试也要用到,继续来写4种排序算法.因为那天用java写了排序,突然想到我是要面试IOS,起码也得用C写.C竟然忘干净了,方法都不会写了.囧啊! 下面用C实现4种排序算法:快速排序.冒 ...
- MTK机器原始OTA更新方法
在源码中编译完成后会生成各类.img的文件,这时候make otapackage生成ota包 一般ota包在源码工程的out/target/...目录下 一.通过线刷模式 将生成OTA包拷贝到Wind ...
- (三十七)从私人通讯录引出的细节I -Notification -Segue -HUD -延时
细节1:账号和密码都有值的时候才可以点击登录按钮,因此应该监听文本框的文本改变. 因为文本框的文本改变代理不能处理,因此应该使用通知Notification. 文本框文本改变会发出通知:通知的前两个参 ...