413. Reverse Integer【easy】
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).
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】的更多相关文章
- 344. Reverse String【easy】
344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...
- 413. Reverse Integer【LintCode java】
Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 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 ...
- 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 ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
随机推荐
- 【JSP JSTL】<c:if>多个判断条件 + <c:foreach>满足条件跳出循环
有一个需求,将所拥有的权限存放于session中,现在JSP页面判断这些如果在所有权限中有某一个或者某几个,就显示相对应的页面内容 举一个例子,实现以上的逻辑,<c:if>多个判断条件 + ...
- pca主成份分析方法
1.应用pca的前提 应用pca的前提是,连续信号具有相关性.相关性是什么,是冗余.就是要利用pca去除冗余. 2.pca的定义 pca是一种去除随机变量间相关性的线性变换.是一种常用的多元数据分析方 ...
- 花间美人:古风 CG插画技法
<花间美人:古风 CG插画技法> 基本信息 作者: 猫君大白 出版社:机械工业出版社 ISBN:9787111429432 上架时间:2013-7-3 出版日期:2013 年7月 开 ...
- 四、logback日志
加入在main\resources 创建logback.xml文件 <configuration> <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, ...
- 你以为border-radius只是圆角吗?【各种角度】
border-radius,国内翻译成圆角,你可能以为这个属性就是用来画圆角,没错,但是除此之外,它还可以做点别的事情. radius其实指的是边框所在圆的半径,这个CSS3属性不仅能够创建圆角,还可 ...
- 使用maven命令搭建多模块企业级项目
http://www.cnblogs.com/xdp-gacl/p/4242221.html
- Centos 7 搭建蓝鲸3.1.5社区版
第一次搭建蓝鲸平台,参考了蓝鲸社区的官方搭建文档. 友情链接:蓝鲸智云社区版V3.1用户手册 搭建时遇到了不少的坑,这里做一个详细的安装梳理 主机硬件要求 官方的推荐如下: 我在公司测试环境搭建时机器 ...
- python网站
https://github.com/search?utf8=%E2%9C%93&q=python+sockethttps://github.com/search?p=2&q=pyth ...
- react-redux 中 connect 的常用写法
1.方式一 export default connect(({ monitorRedux })=>{ return { data: monitorRedux.data } })(Monitor) ...
- Android资源图片读取机制
在新建一个Android项目时.在res目录下会自己主动生成几个drawable目录,drawable-ldpi,drawable-mdpi,drawable-hdpi,一直以来都对此不太清楚.图片应 ...