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题目总结-滑动窗口法 滑动窗口法:此方法首先建立一个长度为零的窗口,把右侧窗口向右移动,当新的元素与原来窗口中的元素不重复时,把新的元素加入其中,并更新窗口长度:当新的元素与原集合中的 ...
随机推荐
- 设置open_cursors参数
1.进入终端,输入命令:sqlplus /nolog 2.输入命令:conn /as sysdba 3.输入命令:alter system set open_cursors=1000 scope=me ...
- 学习PHP垃圾回收机制了解引用计数器的概念
php变量存在一个叫"zval"的变量容器中,"zval"变量容器包括含变量的类型和值,还包括额外的两个字节信息,分别是“is_ref”表示变量是否属于引用,“ ...
- [Eth]网络查看命令:route
最近在调试网络,出现问题,两个网口分别接外网内网,结果不同 http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html
- Android基础总结(八)Service
服务两种启动方式(掌握) startService 开始服务,会使进程变成为服务进程 启动服务的activity和服务不再有一毛钱关系 bindService 绑定服务不会使进程变成服务进程 绑定服务 ...
- [插件] 如何在一个页面中使用多个SWFUpload对象上传文件
首先需要引入相应的样式和JS文件,还需要借助jQuery的js 提供下载路径:http://pan.baidu.com/s/1EUzca ① 引入js <script type="te ...
- hdu 1180:诡异的楼梯(BFS广搜)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- Linux服务器的最大内存和CPU数
从网上查了很多资料.总算把linux下的内存和cpu个数搞清楚了.个人觉得使用linux系统的朋友都应该了解下.先公布如下,如有错误,请反馈给我.谢谢!! Linux系统/服务器能够支持的最大内存和C ...
- JNI 各类数据类型处理
JNI和java数据类型转换: 1.基本数据类型下面一张表是描述了 Java 基本数据类型和JNI中基本数据类型的相对应关系已经占用空间大小. 随便观察就能发现,其实就基本数据类型而已,JNI基本数据 ...
- 剑指 offer set 27 赋值运算符函数
要求为类 CMyString 定义赋值运算符函数. 类的定义如下 class CMyString { public: CMyString(char* pData = NULL; ) CMyString ...
- String、StringBuffer与StringBuilder区别
1.三者在执行速度方面的比较:StringBuilder > StringBuffer > String 2.String <(StringBuffer,StringBuild ...