7_Reverse Integer
7.Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321Example 2:
Input: -123
Output: -321Example 3:
Input: 120
Output: 21Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
没有想出来, 仿照(或者照搬?)http://www.cnblogs.com/grandyang/p/4125588.html来写
注意点:
反转溢出问题, 解决方法: 使用比该数字范围大的类型定义返回值
版本一: 拆分每个位的思想是我的, 其他细节参考上述博客
class Solution {
public:
int reverse(int x) {
long long res = 0;
vector<int> bit;
while (0 != x) {
bit.push_back(x % 10);
x /= 10;
}
for (int i = 0; i < bit.size(); i++) {
res =res * 10 + bit[i];
}
return (res < INT_MIN || res > INT_MAX) ? 0 : res;
}
};
版本二: 半个自己写的, 考虑符号, 虽然多余了
class Solution {
public:
int reverse(int x) {
long long ret = 0;
// int positive = 1;
long long positive = 1;
if (x < 0) {
positive = -1;
x *= positive;
}
while (x != 0) {
ret = ret * 10 + x % 10;
x /= 10;
}
ret *= positive;
return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};
class Solution {
public:
int reverse(int x) {
long long int ret = 0;
while(0 != x) {
ret = ret*10 + x%10;
x /= 10;
}
//return (ret > INT_MIN || ret < INT_MAX)? ret : 0;
return (ret < INT_MIN || ret > INT_MAX) ? 0 : ret;
}
};
7_Reverse Integer的更多相关文章
- 9_Palindrome Number
9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
随机推荐
- spring-boot-route(七)整合jdbcTemplate操作数据库
在一部分内容中,我们学习了Restful接口的编写,及接口文档的生成.我们需要将接口数据进行持久化存储,这一部分我们主要学习几种持久化框架将数据进行存储.本部分内容中,我们都将使用mysql为例来做为 ...
- HTML中css水平居中的几种方式
1. 子元素为行内元素时,父元素使用 text-align: center; 实现子元素的水平居中: 2. 子元素为块级元素时, 2.1. 将子元素设置 margin: 0 auto; 实现居中: 2 ...
- python图像的绘制
转载:https://blog.csdn.net/haoji007/article/details/52063168 实际上前面我们就已经用到了图像的绘制,如: io.imshow(img) 这一行代 ...
- 完全小白入门:python的下载和安装
1. 打开官网www.python.org,选择Downloads
- React踩坑笔记 —— React
Webpack提供了自己的导入方式require.include,但同时也支持commonjs规范或AMD规范的require语法,而Node.js使用的就是common.js,ES6的语法Impor ...
- 这就是小学生也会用的四则计算练习APP吗?- by软工结对编程项目作业
结对编程项目 软件工程 这就是链接 作业要求 这就是链接 作业目标 熟悉在未结对情况下如何结对开发项目 Github与合作者 合作者(学号): 区德明:318005422 虚左以待 Github链接: ...
- 【LGR-070】洛谷 3 月月赛-官方题解
本次免费为大家提供[LGR-070]洛谷 3 月月赛的官方题解,点个赞再走呗! 代码就不上了,大家可以到别的博客上去找找!希望这篇博客能对你有所帮助!
- python之多态
python之多态 一.什么是多态? 同一种事物,有多种形态! class Animal: #同一类事物:动物 def talk(self): pass class Cat(Animal): #动物的 ...
- Redis哨兵知识点总结
1.Redis哨兵介绍 sentinal,中文名是哨兵 A.哨兵是redis集群架构中非常重要的一个组件,主要功能如下 集群监控,负责监控redis master和slave进程是否正常工作 消息通知 ...
- day27 Pyhton 面向对象02 类和对象的命名空间
一.内容回顾 类:具有相同属性和方法的一类事务 # 描述一类事务轮廓的一个机制 #商品/用户/店铺 对象/实例: 对象(实例)就是类的实例化 # 对象就是类的一个具体的表现 #某一件特定的商品/某个人 ...