【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 ...
随机推荐
- python19 操作mysql
connect 模块下载 https://dev.mysql.com/downloads/connector/python/ import mysql.connector con = mysql.co ...
- error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLASS64
今天部署一个探针在运行的时候报了这样一个错:error while loading shared libraries: libstdc++.so.5: wrong ELF class: ELFCLAS ...
- Linux—yum的python版本错误——初级解决方案
为了安装rrdtool,发现不是少这个就是少那个,最后发现yum也不能用. 从网上找的解决yum问题. 转自:http://doarthon.blog.51cto.com/3175384/728809 ...
- CSS上下左右居中对齐
上下左右居中对齐 display: inline/inline-block 将父元素(容器)设定 text-align: center: 即可左右置中. display: block 将元素本身的 ...
- 巩固javaweb的第二十天
巩固内容: 同一个页面中的多个 form 在同一个页面中可以有多个 form 如果存在多个 form,那么提交信息的时候提交哪些信息,提交给哪个文件处理,这都 与提交按钮的位置有关.如果提交按钮在第一 ...
- 零基础学习java------21---------动态代理,java8新特性(lambda, stream,DateApi)
1. 动态代理 在一个方法前后加内容,最简单直观的方法就是直接在代码上加内容(如数据库中的事务),但这样写不够灵活,并且代码可维护性差,所以就需要引入动态代理 1.1 静态代理实现 在讲动态代理之前, ...
- 重磅丨腾讯云开源业界首个 etcd 一站式治理平台 Kstone
Kstone 开源 在 CNCF 云原生基金会举办的2021年12月9日 KubeCon China大会上,腾讯云容器 TKE 团队发布了 Kstone etcd 治理平台开源项目. Kstone ...
- Web安全学习二
目录 常见漏洞攻防 SQL注入 注入分类 按技巧分类 按获取数据的方式分类 注入检测 权限提升 数据库检测 绕过技巧 CheatSheet SQL Server Payload MySQL Paylo ...
- c学习 - 第八章:函数
8.7 数组作函数的参数 1.数组元素作函数的参数--值传递,单向传递 2.数组名做函数的参数--地址传送 (1)实参:数组名做实参,传递的是数组首元素的地址 (2)形参:使用同类型的数组名或指针变量 ...
- OSGI与Spring结合开发web工程
简介: 作为一个新的事实上的工业标准,OSGi 已经受到了广泛的关注, 其面向服务(接口)的基本思想和动态模块部署的能力, 是企业级应用长期以来一直追求的目标.Spring 是一个著名的 轻量级 J2 ...