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 ...
随机推荐
- keepass+坚果云管理我的密码
目录 前言 下载安装KeePass 创建一个数据库 配置坚果云 手机用坚果云 总结 前言 KeePass是一款免费.小巧.绿色且开源的密码管理工具,多年来一直深受大众的好评,它能为用户提供一个 ...
- 萌新学python
python python安装 进入官网http://www.python.org/download/ 下载 我下的是3.6.6大家可以根据需要下载(3.x和2.x不兼容请小心) 之后安装就可以了 p ...
- Docker笔记6:Docker 常见命令及镜像管理
目 录 一.Docker 常用命令 docker version 命令 docker info 命令 二.Docker 镜像管理 搜索镜像:docker search 镜像名 获取镜像:docker ...
- mycat相关配置文件和参数解析
#vi /usr/local/mycat/conf/schema.xml<!--######################################################### ...
- Java 移位运算、符号位扩展
类型取值范围 short 是1字节,即8位.而且 Java 中只有有符号数,所以最大值 0111,1111=2^7-1. 同时计算机中以补码形式存负数,所以可以多表示一个数,则最小值 1000,000 ...
- centOS7 查看防火墙状态 开放端口
一.防火墙的开启.关闭.禁用命令 (1)设置开机启用防火墙:systemctl enable firewalld.service (2)设置开机禁用防火墙:systemctl disable fire ...
- Mosquitto服务器的日志分析
启动Mosquitto后,我们可以看到Mosquitto的启动日志: 1515307521: mosquitto version 1.4.12 (build date 2017-06-01 13:03 ...
- MySQL数据库基础-1
数据库原理 数据时代 信息创造价值 -结构化数据 关系完整,密切 -非结构化数据 数据散乱,相互关系不大 -半结构化数据 XML HTML 也不是完全没有结构,也不是特别规矩 MySQL适合管理结构化 ...
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- sql优化整理(二)
对于连接查询,EXPLAIN的extra字段出现using join buffer,表示使用了连接缓存,保证JOIN语句中被驱动表上JOIN条件字段已经添加索引: LEFT JOIN 条件用于确定如何 ...