LeetCode题目_Reverse Integer
最近在LeetCode上做题,写点东西记录一下,虽然自己做的都是些很水的题目,但是重在练手。
题号7:Reverse Integer,题目描述:
Reverse digits of an integer:例如,输入123,输出321;输入-123,输出-321。
思路很简单:将原数的每一位求出,然后将原数的每一位倒序,生成一个整数。在程序中,使用了队列,利用其先进先出的原则,倒序原数每一位。
注意的地方:防止溢出。
代码如下:
class Solution {
public:
int reverse(int m) {
long long n =(long long)m;
queue<long> q;
if(n==0)
return 0;
else
{
if(n>0)
{
while(n)
{
q.push(n%10);
n/=10;
}
while(!q.empty())
{
n=n*10+q.front();
q.pop();
}
if(n>2147483647) return 0;
return n;
}
else
{
n=abs(n);
while(n)
{
q.push(n%10);
n/=10;
}
while(!q.empty())
{
n=n*10+q.front();
q.pop();
}
if(abs(n)>2147483648UL) return 0;
return -n;
}
}
}
};
LeetCode题目_Reverse Integer的更多相关文章
- [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题目清单
2016-09-24,开始刷leetcode上的算法题,下面整理一下leetcode题目清单.Github-leetcode 1.基本数学 Two Sum Palindrome Number Cont ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- LeetCode 题目总结/分类
LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.l ...
- Binary Search Tree 以及一道 LeetCode 题目
一道LeetCode题目 今天刷一道LeetCode的题目,要求是这样的: Given a binary search tree and the lowest and highest boundari ...
- Leetcode题目practice
目录 Leetcode题目解答 1. 删除最外层的括号 2. 两数之和 3. 宝石与石头 4. 移除元素 5.删除排序数组中的重复项 6.寻找两个有序数组的中位数 7.盛最多水的容器 8.存在重复元素 ...
- LeetCode题目总结-滑窗法
LeetCode题目总结-滑动窗口法 滑动窗口法:此方法首先建立一个长度为零的窗口,把右侧窗口向右移动,当新的元素与原来窗口中的元素不重复时,把新的元素加入其中,并更新窗口长度:当新的元素与原集合中的 ...
随机推荐
- 读取SD卡中的图片
Touxiang=(ImageView)view.findViewById(R.id.Touxiang); //头像Bitmap bm = BitmapFactory.decodeFile(" ...
- FreeRTOS 任务优先级分配方案
任务优先级说明下面对 FreeRTOS 优先级相关的几个重要知识点进行下说明,这些知识点在以后的使用中务必要掌握牢固. FreeRTOS 中任务的最高优先级是通过 FreeRTOSConfig.h ...
- c# 程序调试出现“未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。”
简单的程序代码如下:DataSet ds=new DataSet();try{ string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data ...
- 搭建springmvc框架的另一种思路
在一个完整的项目里搭建springmvc框架的时候, 通常情况下,初学者在配置的时候,总是会把"中央控制器的名字"-servlet.xml文件放到/Webroot/WEB-INF下 ...
- PHP——转义字符
链接:百度-转义字符 http://baike.baidu.com/link?url=obfdOqATx4TO0Ev_kFnPz37wwW3SDhFPsvNobVTidhFuCn2zK5VmCuW1L ...
- js 刷新后不提示并保留控件状态
保存后,想提示一下并保留查询条件的状态,发现可以用document.forms[0].submit();继续提交达到刷新的目的 代码如下: ScriptManager.RegisterStartupS ...
- 解决request.getRemoteAddr()获取的值为0:0:0:0:0:0:0:1这个小问题
症状: Windows操作系统,eclipse开发环境下,在本机上使用http://localhost:8080/...访问本机上的页面,使用tomcat作为服务器 在Servlet或者Action中 ...
- 在堆栈中,push为入栈操作,pop为出栈操作
LinkedList提供以下方法:(ArrayList无此类方法) addFirst(); removeFirst(); addLast(); removeLast(); 在堆栈中,push为入栈操作 ...
- 辛星和您一起解析PHP中的单例模式
事实上单例模式还是用的挺多的,要说到最经典的样例.可能就是操纵数据库的类了,它假设是单例的话,能够避免大量的new操作消耗资源,而假设系统中须要一个类来管理全局的信息,则把它用成单例也是非常不错的.由 ...
- 从myspace数据库看分布式系统数据结构变迁[转]
MySpace已经成为全球众口皆碑的社区网站之王.尽管一流和营销和管理经验自然是每个IT企业取得成功的首要因素,但是我们却抛弃这一点,而主要着眼于探讨在数次面临系统扩张的紧急关头MySpace是如何从 ...