最近在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. FreeRTOS 中断优先级配置(重要)

    以下转载自安富莱电子: http://forum.armfly.com/forum.php NVIC 的全称是 Nested vectored interrupt controller,即嵌套向量中断 ...

  2. 记一次线上MySQL数据库死锁问题

            最近线上项目报了一个MySQL死锁(DealLock)错误,虽说对业务上是没有什么影响的,由于自己对数据库锁这块了解不是很多,之前也没怎么的在线上碰到过.这次刚好遇到了,便在此记录一下 ...

  3. [C++]using std string;的作用是什么

    相关资料: http://bbs.csdn.net/topics/330194465 #include <string>将string库包含到当前编译单元中. using std::str ...

  4. Javascript知识点:IIFE - 立即调用函数

    Immediately-invoked Function Expression(IIFE,立即调用函数),简单的理解就是定义完成函数之后立即执行.因此有时候也会被称为“自执行的匿名函数”(self-e ...

  5. 专题实验 Toad 用户的创建与管理( 包括 role 等 )

    1. 用户登录数据库 是否可以通过操作系统权限来登录数据库, $ORACLE_HOME/network/admin/sqlnet.ora 这个文件中设置, 如果增加参数sqlnet.authentic ...

  6. 打印-print.js

    //打印开始// strPrintName 打印任务名// printDatagrid 要打印的datagridfunction CreateFormPage(ctx,strPrintName, pr ...

  7. IOC控制反转

    IOC是Inversion of Control的缩写,多数书籍翻译成“控制反转”,还有些书籍翻译成为“控制反向”或者“控制倒置”.     1996年,Michael Mattson在一篇有关探讨面 ...

  8. Discretized Streams, 离散化的流数据处理

    Discretized Streams: An Efficient and Fault-Tolerant Model for Stream Processing on Large Clusters   ...

  9. 关于Unity中的新手编码技巧

    写代码遇到报错,问题怎么办?怎么查看unity代码的接口?函数参数不记得了怎么办? 解决方法: 1.选择不懂的函数或类,按F12,跳转到代码的定义,自己去看就可知道了. 2.有的时候,选择一个函数,按 ...

  10. @Bean 小知识

    先说结论 @Bean 可以用在任意方法上. -- 也可以用在注解上面. @Bean 仅在Spring创建bean时起作用. 这应该算一个小技巧,在一个平常类(非@Configuration class ...