【LeetCode每天一题】Reverse Integer(反转数字)
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 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.
思路:对于负数设置一个标志位,然后将其转化成正整数,再来计算,最后判断大小是否超过了[−231, 231 − 1]这个范围。时间复杂度为O(log 10 x), 空间复杂度为O(1)。
代码
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
if x == : # 为0直接返回
return
res, neg_flag = ,False # 设置结果和负数标志位
if x < :
neg_flag = True
x = abs(x)
while x: # 进行反转
tem = x %
x = x//
res = (res* + tem)
if neg_flag: # 判断舒服标志位,并进行相应的转换
res = -res
if res > pow(,)- or res < -pow(,): # 判断是否超出范围, 直接返回0
return
return res # 返回结果
【LeetCode每天一题】Reverse Integer(反转数字)的更多相关文章
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- leetcode第七题--Reverse Integer
Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- LeetCode记录之7——Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- 【LeetCode算法-7】Reverse Integer
LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- Leetcode 题目整理-2 Reverse Integer && String to Integer
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
随机推荐
- uni-app,wex5,APPcan,ApiCloud几款国内webapp开发框架的选型对比
框架列表. https://www.cnblogs.com/xiaxiaxia/articles/5705557.html 前言 近期,要开一个新的项目,APP类型.最重要的需求就是能够随时调整APP ...
- 关于device tree中的interrupts选项
http://blog.csdn.net/jiang__jiang/article/details/52064715 随着Linux的发展,dts技术是大势所趋.里面的interrupts = < ...
- LeetCode 112 Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- TOP100summit:【分享实录】京东1小时送达的诞生之路
本篇文章内容来自2016年TOP100summit 京东WMS产品负责人李亚曼的案例分享. 编辑:Cynthia 李亚曼:京东 WMS产品负责人.从事电商物流行业近10年,有丰富的物流行业经验,独立打 ...
- Unable to cast object of type 'System.Int32' to type 'System.Array'.
x 入职了新公司.最近比较忙...一看博客...更新频率明显少了...罪过罪过... 新公司用ASP.NET MVC 遇上一个错误: Unable to cast object of type 'Sy ...
- [No0000106]配置PLSQL,提升工作效率
界面模板的配置: 方便用户快速点击需要的功能.如打开SQL Window 1.打开customize,用户自定义Toolbars对话框. 2.在Commands命令标签页,选中要添加的命令,拖动到工具 ...
- MySQL8.0安装连接Navicat的坑
刚在官网装好MySQL8.0后,我的cmd识别不了启动数据库的指令 需要cd到MySQL的bin目录配置mysql mysqld --install mysqld --remove mysql -u ...
- go,gcvis,golang, privoxy,and git proxy
/etc/init.d/privoxy restartexport http_proxy=http://127.0.0.1:8118export https_proxy=http://127.0.0. ...
- c# http get post转义HttpUtility.UrlEncode
//该数据如果要http get.post提交,需要经过转义,否则该数据中含& ''等字符会导致意外错误.需要转义.这里用HttpUtility.UrlEncode来转义.接收方无需反解析 s ...
- springmvc shiro整合cas单点登入
shiro cas分为登入跟登出 maven依赖: <dependency> <groupId>org.apache.shiro</groupId> <art ...