[LintCode] Reverse Integer 翻转整数
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
LeetCode上的原题,请参见我之前的博客Reverse Integer。
解法一:
class Solution {
public:
/**
* @param n the integer to be reversed
* @return the reversed integer
*/
int reverseInteger(int n) {
long long res = ;
while (n != ) {
res = * res + n % ;
n /= ;
}
return (res < INT_MIN || res > INT_MAX) ? : res;
}
};
解法二:
class Solution {
public:
/**
* @param n the integer to be reversed
* @return the reversed integer
*/
int reverseInteger(int n) {
int res = ;
while (n != ) {
int t = res * + n % ;
if (t / != res) return ;
res = t;
n /= ;
}
return res;
}
};
[LintCode] Reverse Integer 翻转整数的更多相关文章
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- lintcode :reverse integer 颠倒整数
题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...
- [leetcode]7. Reverse Integer反转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- 7. Reverse Integer[E]整数反转
题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- 7. Reverse Integer 反转整数
[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- [LintCode] Reverse Pairs 翻转对
For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return to ...
随机推荐
- Arduino101学习笔记(二)—— 一些注意的语法点
1.宏定义 2.整数常量 3.支持C++ String类 (1)String 方法 charAt() compareTo() concat() endsWith() equals() equalsIg ...
- sqoop中,如果数据中本身有换行符,会导致数据错位
sqoop中,如果数据中本身有换行符,会导致数据错位: 解决办法: 在sqoop import时修改配置文件 sudo -u hive sqoop import --connect jdbc:mysq ...
- Handler(消息机制)
Demo演示 //通过Handler事件倒计时的一个操作,并判断状态 public class MainActivity extends AppCompatActivity {private Text ...
- jade学习01
编写 简单例子 doctype html html head title learn jade body h1 learn jade 常用命令 编译: jade index.jade //默认编译成压 ...
- Transform组件C#游戏开发快速入门
Transform组件C#游戏开发快速入门大学霸 组件(Component)可以看作是一类属性的总称.而属性是指游戏对象上一切可设置.调节的选项,如图2-8所示.本文选自C#游戏开发快速入门大学霸 ...
- 移动开单扫描终端-全触屏互联网安卓打印扫描 PDAPOS机——开单扫描POS-移动开单扫描POS
浩瀚云POS是一款自主研发的全触屏互联网POS,一贯以领先的技术.快捷友好的用户体验和在全国广泛覆盖的网络,为电子商务.提供安全.可靠.保密的数据校验服务.作为其研发的一款专业移动开单扫描终端. 互联 ...
- 【bzoj2440】【bzoj3994】莫比乌斯反演学习
哇..原来莫比乌斯代码这么短..顿时感觉逼格-- 写了这道题以后,才稍稍对莫比乌斯函数理解了一些 定理:和是定义在非负整数集合上的两个函数,并且满足条件,那么我们得到结论 在上面的公式中有一个函数,它 ...
- struts2总结二:第一个简单的struts2程序
到struts2官网上面下载struts2的jar包,然后解压. struts2的入门程序,实现简单的用户登录,struts2里面的helllo world.利用eclipse的开发工作如下: 1.首 ...
- Money类型转化为String去除小数点后0解决方法
Money类型转化为String去除小数点后0从数据库提取Money类型后,字符串如:1212.0000 如何使其成为1212 注:去掉了小数点 如果是:1212.0100 使 ...
- BZOJ4117 : [Wf2015]Weather Report
一种天气情况的概率只与4种天气的出现次数有关,故将相同概率的情况计数后放入堆中模拟哈夫曼树即可. 每次取出概率最小的,将它个数除以2,对于零头需要特判. #include<cstdio> ...