最近在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的更多相关文章

  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. arm + fpga 核心板

  2. CSS(七):浮动

    一.float属性取值:left:左浮动right:右浮动none:不浮动 先看下面的一个例子: <!DOCTYPE html> <html lang="en"& ...

  3. 回文树(统计所有回文串的个数) - MCCME 1750 Подпалиндромы

    Подпалиндромы Problem's Link: http://informatics.mccme.ru//mod/statements/view.php?chapterid=1750# M ...

  4. Oracle触发器修改数据时同步执行插入该条数据

    原创:lixx ---从UNWIREDATA 表中查询数据,如果该表中地磁状态发生改变(CARDSTATE)执行插入到MAINTABLE 表中---根据唯一值ERID判断,如果ERID值存在,修改该条 ...

  5. Bitcoin: A Peer-to-Peer Electronic Cash System(比特币论文翻译)

    比特币历史: 2008年,比特币论文诞生 2009年1月,第一批比特币诞生 2011年4月,比特币价格第一次达到了1美元 2011年6月,涨到30美元,然后开始跌 2013年1月,4美元 2013年1 ...

  6. csdn 模式识别

    http://blog.csdn.net/liyuefeilong/article/details/45217335 模式识别 http://ceit.ucas.ac.cn/index.php?id= ...

  7. 漫游Kafka设计篇之性能优化(7)

    Kafka在提高效率方面做了很大努力.Kafka的一个主要使用场景是处理网站活动日志,吞吐量是非常大的,每个页面都会产生好多次写操作.读方面,假设每个消息只被消费一次,读的量的也是很大的,Kafka也 ...

  8. 如何在VS2010中更好的编写JavaScript代码

    VS2010默认的JavaScript代码编辑器相对简单.对于大家熟悉的代码折叠,代码结构.函数导航,代码高亮等都不支持,使用很不便.下面介绍下我发现的几个VS2010插件,具有哪些功能,如何安装和使 ...

  9. Hibernate_day02--课程安排_主键生成策略_对实体类crud操作_实体类对象状态

    Hibernate_day02 上节内容 今天内容 实体类编写规则 Hibernate主键生成策略 实体类操作 对实体类crud操作 添加操作 根据id查询 修改操作 删除操作 实体类对象状态(概念) ...

  10. iOS开发之--打印一堆奇怪东西的解决方案

    Xcode8总是打印一堆奇怪的东西.虽然不影响开发,但是看着很不爽.解决的方法就是在Scheme里面添加(name):OS_ACTIVITY_MODE (value):disable 然后再在突出的框 ...