413-反转整数

将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。

样例

给定 x = 123,返回 321

给定 x = -123,返回 -321

标签

整数

思路

利用取余和除法取出整数每一位,需要注意整数越界问题

code

class Solution {
public: /*
* @param n: the integer to be reversed
* @return: the reversed integer
*/
int reverseInteger(int n) {
// write your code here
bool isNeg = (n < 0);
n = abs(n);
long long res = 0;
do {
res *= 10;
res += n % 10;
n = n / 10;
}
while (n > 0);
if (res > INT_MAX) {
res = 0;
}
if (isNeg == true) {
return -1 * res;
}
return res;
}
};

lintcode-413-反转整数的更多相关文章

  1. LeetCode 4.反转整数

    给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: ...

  2. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  3. lintcode413 反转整数

    反转整数   将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 您在真实的面试中是否遇到过这个题? Yes 样例 给定 x = 123,返回 321 给定 x = ...

  4. leetcode每日一题——反转整数

    题目: 反转整数 难度: 简单 描述: 给定一个 32 位有符号整数,将整数中的数字进行反转. 解法: class Solution { public int reverse(int x) { //i ...

  5. LeetCode 7. 反转整数(Reverse Integer)

    题目描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 ...

  6. [Swift]LeetCode7. 反转整数 | Reverse Integer

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

  7. leetcode7:反转整数

    给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...

  8. Leetcode 7.反转整数 By Python

    给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...

  9. [LeetCode] 反转整数

    题目: 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注 ...

  10. LeetCode7.反转整数

    给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...

随机推荐

  1. c# 在WebBrowser中用SendMessage模拟鼠标点击

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. python的MetaClass的代码分析。基于廖雪峰博客代码

    # 一张表一个类,表内每一行就是一个实例 ''' 一个单独的元类使用的程序分析. ''' class Field(object): def __init__(self, name, column_ty ...

  3. shiro实战整合

    引入依赖(包括缓存等): <!-- SECURITY begin --> <dependency> <groupId>org.apache.shiro</gr ...

  4. noone is not in the sudoers file ubuntu

      Login as root or su to get root prompt type visudo an editor will open find a line says root ALL=( ...

  5. Kali linux更新源

    1.更新软件源: 修改sources.list文件: leafpad /etc/apt/sources.list 然后选择添加以下适合自己较快的源(可自由选择,不一定要全部): #官方源deb htt ...

  6. Odoo中创建模块语句

    使用odoo的odoo-bin命令创建模块,比较方便. 进入终端界面(windows中可以是cmd中,linux中可以是$命令提示符下),以下在Windows中为例: python odoo-bin ...

  7. PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar

    本文目的:展示 PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar. 本人搜遍所有网络无果,没办法,查看PyQt5源代码,最终才搞明白...特此留记. 〇.Py ...

  8. day1 Ubuntu 使用

    ctrl + shift + +   放大终端 ctrl + -   缩小终端 软连接,硬链接   ln python@ubuntu:~/Desktop$ vim .txt python@ubuntu ...

  9. scala : 类型与类

    scala类型系统:1) 类型与类 在Java里,一直到jdk1.5之前,我们说一个对象的类型(type),都与它的class是一一映射的,通过获取它们的class对象,比如 String.class ...

  10. 【转】ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.165' (113)

    原文转自:http://blog.csdn.net/chengyuqiang/article/details/54285857 1.程序报错: com.mysql.jdbc.exceptions.jd ...