LeetCode Algorithm 07_Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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.
class Solution {
public:
int reverse(int x) {
bool isNegative = false;
if(x<){
x=-x;
isNegative = true;
}
unsigned long result = ;
while(x){
result = result* + x%;
x = x/;
}
if(result > INT_MAX) return ;
return (isNegative)? int(-result) : int(result) ;
}
};
LeetCode Algorithm 07_Reverse Integer的更多相关文章
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
- LeetCode Algorithm 05_Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 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 ...
随机推荐
- 深度学习2015年文章整理(CVPR2015)
国内外从事计算机视觉和图像处理相关领域的著名学者都以在三大顶级会议(ICCV.CVPR和ECCV)上发表论文为荣,其影响力远胜于一般SCI期刊论文.这三大顶级学术会议论文也引领着未来的研究趋势.CVP ...
- zoj1942Frogger
Frogger Time Limit: 2 Seconds Memory Limit: 65536 KB Freddy Frog is sitting on a stone in the m ...
- 为什么我们须要复杂的password
前两天我打开邮箱一看.收到公司1331一封要求改动邮箱password的邮件. 为什么我们须要一个复杂的password呢?尽管我一直以来设置的password都非常复杂.可是公司这次要求改动pass ...
- DSP开发中遇到的问题 - 类指针未初始化后果
收到RECEIVE_REQ_MSG消息时会运行以下的代码,这里由于某种原因m_receiverSlaverController的值仍为NULL,并没有指向详细的CReceiverSlaverContr ...
- 分享js寄生组合模式继承
function person(){ this.name = 'taobao'; this.showMess = function(){ return this.name; } } person.pr ...
- Vue 实现分页+输入框关键字筛选
分页的实现(Vue+Element)+输入框关键字筛选 1.这里用的是Element 自带的分页组件 <template> <div class="sales-table& ...
- 【Uva 1632】Alibaba
[Link]: [Description] 直线上有n(n≤10000)个点,其中第i个点的坐标是xi,且它会在di秒之后消失.Alibaba 可以从任意位置出发,求访问完所有点的最短时间.无解输出N ...
- Linux 获取上个月的第一秒和上个月的最后一秒
因为写脚本需求须要获得上个月的第一秒和上个月的最后一秒,查阅了相关资料,并通过自己实践.找到了以下这样的方法能满足要求.在此备注,若有其它好的方法.请留言.本人将不胜感激. 获取上个月的第一秒: da ...
- 旧知识打造新技术--AJAX学习总结
AJAX是将旧知识在新思想的容器内进行碰撞产生的新技术:推翻传统网页的设计技术,改善用户体验的技术. 学习AJAX之初写过一篇<与Ajax的初次谋面>.当中都仅仅是一些自己浅显的理解.这次 ...
- 20.Node.js EventEmitter的方法和事件
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html EventEmitter 提供了多个属性,如 on 和 emit.on 函数用于绑定事件函数, ...