【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
公众号:负雪明烛
本文关键词:整数,反转,题解,Leetcode, 力扣,Python, C++, Java
题目地址:https://leetcode.com/problems/reverse-integer/description/
题目描述
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
- Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
题目大意
把一个 int 进行翻转,如果翻转后的数值不在 int 范围内,返回0。
解题方法
转成字符串
题目要对整数进行翻转,并说明了当翻转了的整数超过了32位,就是用0替代。
这个题又让我学会了一个新的函数bit_length(),在python2.7以上可以对整型使用。
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
n = cmp(x, 0) * int(str(abs(x))[::-1])
return n if n.bit_length() < 32 else 0
二刷,同样使用字符串翻转,只不过翻转之后进行判断直接使用使用int最大值和最小值。
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x == 0: return 0
flag = 1
if x < 0:
flag = -1
x = -x
r = int(str(x)[::-1])
if flag == 1 and r <= 2147483647:
return r
elif flag == -1 and -r >= -2147483648:
return -r
return 0
C++版本的代码如下:
class Solution {
public:
int reverse(int x) {
if (x == 0) return 0;
int flag = 1;
if (x < 0) flag = -1;
string xs = to_string(x);
long r = stol(string(xs.rbegin(), xs.rend()));
if (flag == 1 && r <= INT_MAX) return r;
if (flag == -1 && -r >= INT_MIN) return -r;
return 0;
}
};
数学
TODO
日期
2018 年 2 月 5 日
2018 年 11 月 30 日 —— 又到了周末
2021 年 8 月 7 日
【LeetCode】7. Reverse Integer 整数反转的更多相关文章
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- [LeetCode]7. Reverse Integer整数反转
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- Leetcode7 : Reverse Integer 整数反转问题
问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Leetcode#344. Reverse String(反转字符串)
题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
随机推荐
- Go语言缺陷
我为什么放弃Go语言 目录(?)[+] 我为什么放弃Go语言 有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?其实我一直在认真思考这个问题. 开 ...
- C++面试基础篇(一)
1. static关键字的作用 (1)全局静态变量 在全局变量前面加上关键字static, 全局变量就定义为一个全局静态变量 在静态存储区,在整个程序运行期间一致存在. 初始化:未初始化的全局静态变量 ...
- SpringBoot整合Shiro 四:认证+授权
搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...
- 🚀 RabbitMQ课程发布-KuangStudy
RabbitMQ课程上线(44集) 视频教程地址:https://www.kuangstudy.com/course/detail/1323452886432944129 专栏地址:https://w ...
- Linux修改默认挂载NFS协议版本
系统版本:CentOS Linux release 7.4.1708 (Core) $ vi /etc/nfsmount.conf # # /etc/nfsmount.conf - see nfsmo ...
- Spark 广播变量和累加器
Spark 的一个核心功能是创建两种特殊类型的变量:广播变量和累加器 广播变量(groadcast varible)为只读变量,它有运行SparkContext的驱动程序创建后发送给参与计算的节点.对 ...
- IDEA2021.2安装与配置
https://blog.csdn.net/qq_37242720/article/details/119349394
- Oracle中如何自定义类型
一:Oracle中的类型有很多种,主要可以分为以下几类:1.字符串类型.如:char.nchar.varchar2.nvarchar2.2.数值类型.如:int.number(p,s).integer ...
- iOS 的文件操作
直接上操作 效果:将一张图片写入文件 (图片本身已经在Assets.xcassets里面了) 1.获取当前app的沙盒路径 NSString *documentPath = NSSearchPathF ...
- Linux基础命令---ntpq查询时间服务器
ntpq ntpq指令使用NTP模式6数据包与NTP服务器通信,能够在允许的网络上查询的兼容的服务器.它以交互模式运行,或者通过命令行参数运行. 此命令的适用范围:RedHat.RHEL.Ubuntu ...