Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).

 
Example

Given x = 123, return 321

Given x = -123, return -321

解法一:

 class Solution {
public:
/*
* @param n: the integer to be reversed
* @return: the reversed integer
*/
int reverseInteger(int n) {
long ret = ; while (n != ) {
ret = ret * + n % ;
n /= ;
} if (ret > INT_MAX || ret < INT_MIN) {
return ;
} return (int)ret;
}
};

先用long来计算防止越界,然后再把越界的处理掉。

解法二:

 class Solution {
public:
int reverseInteger(int x) {
int rst = ; while(x != ){
int next_rst = rst * + x % ;
x = x / ;
if(next_rst / != rst) {
rst = ;
break;
}
rst = next_rst;
}
return rst;
}
};

参考@NineChapter 的代码

413. Reverse Integer【easy】的更多相关文章

  1. 344. Reverse String【easy】

    344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...

  2. 413. Reverse Integer【LintCode java】

    Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 345. Reverse Vowels of a String【easy】

    345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...

  5. 557. Reverse Words in a String III【easy】

    557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...

  6. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  7. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  8. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  9. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

随机推荐

  1. 什么是webview

    WebView(网络视图)能加载显示网页,可以将其视为一个浏览器.它使用了WebKit渲染引擎加载显示网页,实现WebView有以下两种不同的方法:第一种方法的步骤:1.在要Activity中实例化W ...

  2. Openshift部署Zookeeper和Kafka

    部署Zookeeper github网址 https://github.com/ericnie2015/zookeeper-k8s-openshift 1.在openshift目录中,首先构建imag ...

  3. lua调试的工具选择

    近期看到一个关于vs的lua调试插件, 装了vs2012试了下, 忍不住发此文总结下lua各种调试工具 Decoda 这是现今地球上调试lua5.1最方便的工具, 没有之中的一个. 强大的注入式调试, ...

  4. WordPress < 3.6.1 PHP 对象注入漏洞

    0x00 背景 当我读到一篇关于Joomla的“PHP对象注射”的漏洞blog后,我挖深了一点就发现Stefan Esser大神在2010年黑帽大会的文章: http://media.blackhat ...

  5. 提取nds游戏的音乐

    nds游戏绝对是游戏界的瑰宝,尤其是有些游戏的音乐还很好听,所以想把它们提取出来.网上搜了下教程,这里整理一下,全程在archlinux下操作. 首先用到的工具是(vgmtrans)[https:// ...

  6. Node.js 2017.11.5-2017.11.16期间制作的图片爬虫总结

    2017年11月18日12:33:06

  7. Android传感器的介绍

    在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROMETER       1 //加速度#de ...

  8. C#字符串来袭——因为爱,所以爱

    一直以来都喜欢谢霆锋,喜欢他身上的那股劲,也一直喜欢唱他的歌,比如这首最广为人知的<因为爱,所以爱>:因为爱所以爱,温柔经不起安排,愉快那么快,不要等到互相伤害...是的,没到互相伤害,他 ...

  9. Direcshow中视频捕捉和參数设置报告

    Direcshow中视频捕捉和參数设置报告 1.      关于视频捕捉(About Video Capture in Dshow) 1视频捕捉Graph的构建 一个能够捕捉音频或者视频的graph图 ...

  10. static、final修饰符、内部类

    static修饰符: static修饰符能够与属性.方法和内部类一起使用,表示静态的.类中的静态变量和静态方法能够与类名一起使用.不须要创建一个类的对象来訪问该类的静态成员. class Static ...