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. 【mybatis】mybatis多表联查,存在一对多关系的,实体中使用List作为字段接收查询结果的写法

    实体如下: IntegralGoods  积分商品 IntegralGoodsImg 积分商品图片 ShelfLog 积分商品自动上架记录 IntegralGoods :IntegralGoodsIm ...

  2. Log4j 日志级别

    转自:http://michales003.iteye.com/blog/1160605 日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).: 1.static ...

  3. (转载)如何学好iphone游戏开发

    转自:http://www.cnblogs.com/zilongshanren/archive/2011/09/19/2181558.html 自从发布<如何学习iphone游戏开发>到 ...

  4. acle联机日志文件的维护

    1.刷新重做日志缓存的时机 a.commit b.缓存满了 c.checkpoint,checkpoint的触发有两种机制: 定时触发,由log_checkpoint_interval[1]参数决定间 ...

  5. MFC【17-2】线程和线程同步化

    17-2线程同步 Windows支持4中类型的同步对象,可以用过来同步由并发运行的线程执行的操作: 临界区 互斥量 事件 信号量 MFC在名为CCriticalSection\CMutex\CEven ...

  6. 如何将redis中的数据导入到本地MongoDB和MySQL数据库

    将redis中的数据导入到本地MongoDB数据库 创建一个process_items_mongodb.py文件(文件名自定义): #!/usr/bin/env python # -*- coding ...

  7. KafkaConsumer对于事务消息的处理

    Kafka添加了事务机制以后,consumer端有个需要解决的问题就是怎么样从收到的消息中滤掉aborted的消息.Kafka通过broker和consumer端的协作,利用一系列优化手段极大地降低了 ...

  8. DNS域名解析服务

    一.DNS的体系结构: DNS:域名解析系统 DNS由根域.顶级域和子域构成.根域主要负责管理顶级域,顶级域主要负责管理其下面子域. .代表DNS的根域. .com..edu等代表顶级域. shou. ...

  9. maven install时报错Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test

    事故现场: 解决办法: 一是命令行, mvn clean package -Dmaven.test.skip=true 二是写入pom文件, <plugin> <groupId> ...

  10. Eleven puzzle_hdu_3095(双向广搜).java

    Eleven puzzle Time Limit: 20000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...