最近在LeetCode上做题,写点东西记录一下,虽然自己做的都是些很水的题目,但是重在练手。

题号7:Reverse Integer,题目描述:

Reverse digits of an integer:例如,输入123,输出321;输入-123,输出-321。

思路很简单:将原数的每一位求出,然后将原数的每一位倒序,生成一个整数。在程序中,使用了队列,利用其先进先出的原则,倒序原数每一位。

注意的地方:防止溢出。

代码如下:

  1. class Solution {
  2. public:
  3. int reverse(int m) {
  4.  
  5. long long n =(long long)m;
  6. queue<long> q;
  7. if(n==0)
  8. return 0;
  9. else
  10. {
  11. if(n>0)
  12. {
  13. while(n)
  14. {
  15. q.push(n%10);
  16. n/=10;
  17. }
  18. while(!q.empty())
  19. {
  20. n=n*10+q.front();
  21. q.pop();
  22. }
  23. if(n>2147483647) return 0;
  24. return n;
  25. }
  26. else
  27. {
  28. n=abs(n);
  29. while(n)
  30. {
  31. q.push(n%10);
  32. n/=10;
  33. }
  34. while(!q.empty())
  35. {
  36. n=n*10+q.front();
  37. q.pop();
  38. }
  39. if(abs(n)>2147483648UL) return 0;
  40. return -n;
  41. }
  42. }
  43. }
  44. };

LeetCode题目_Reverse Integer的更多相关文章

  1. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. leetcode题目清单

    2016-09-24,开始刷leetcode上的算法题,下面整理一下leetcode题目清单.Github-leetcode 1.基本数学 Two Sum Palindrome Number Cont ...

  3. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  4. LeetCode 7. Reverse Integer (倒转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  5. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  6. LeetCode 题目总结/分类

    LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.l ...

  7. Binary Search Tree 以及一道 LeetCode 题目

    一道LeetCode题目 今天刷一道LeetCode的题目,要求是这样的: Given a binary search tree and the lowest and highest boundari ...

  8. Leetcode题目practice

    目录 Leetcode题目解答 1. 删除最外层的括号 2. 两数之和 3. 宝石与石头 4. 移除元素 5.删除排序数组中的重复项 6.寻找两个有序数组的中位数 7.盛最多水的容器 8.存在重复元素 ...

  9. LeetCode题目总结-滑窗法

    LeetCode题目总结-滑动窗口法 滑动窗口法:此方法首先建立一个长度为零的窗口,把右侧窗口向右移动,当新的元素与原来窗口中的元素不重复时,把新的元素加入其中,并更新窗口长度:当新的元素与原集合中的 ...

随机推荐

  1. 设置open_cursors参数

    1.进入终端,输入命令:sqlplus /nolog 2.输入命令:conn /as sysdba 3.输入命令:alter system set open_cursors=1000 scope=me ...

  2. 学习PHP垃圾回收机制了解引用计数器的概念

    php变量存在一个叫"zval"的变量容器中,"zval"变量容器包括含变量的类型和值,还包括额外的两个字节信息,分别是“is_ref”表示变量是否属于引用,“ ...

  3. [Eth]网络查看命令:route

    最近在调试网络,出现问题,两个网口分别接外网内网,结果不同 http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html

  4. Android基础总结(八)Service

    服务两种启动方式(掌握) startService 开始服务,会使进程变成为服务进程 启动服务的activity和服务不再有一毛钱关系 bindService 绑定服务不会使进程变成服务进程 绑定服务 ...

  5. [插件] 如何在一个页面中使用多个SWFUpload对象上传文件

    首先需要引入相应的样式和JS文件,还需要借助jQuery的js 提供下载路径:http://pan.baidu.com/s/1EUzca ① 引入js <script type="te ...

  6. hdu 1180:诡异的楼梯(BFS广搜)

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  7. Linux服务器的最大内存和CPU数

    从网上查了很多资料.总算把linux下的内存和cpu个数搞清楚了.个人觉得使用linux系统的朋友都应该了解下.先公布如下,如有错误,请反馈给我.谢谢!! Linux系统/服务器能够支持的最大内存和C ...

  8. JNI 各类数据类型处理

    JNI和java数据类型转换: 1.基本数据类型下面一张表是描述了 Java 基本数据类型和JNI中基本数据类型的相对应关系已经占用空间大小. 随便观察就能发现,其实就基本数据类型而已,JNI基本数据 ...

  9. 剑指 offer set 27 赋值运算符函数

    要求为类 CMyString 定义赋值运算符函数. 类的定义如下 class CMyString { public: CMyString(char* pData = NULL; ) CMyString ...

  10. String、StringBuffer与StringBuilder区别

    1.三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuild ...