【LeetCode】#7 Reverse Integer
【Question】
Reverse digits of an integer.
Example:
x = 123, return 321
x = -123, return -321
【My Solution】
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
neg = 1
if x < 0:
neg = -1
if x == 0:
return 0
x = abs(x)
r = 0
while x > 0:
r *= 10
r += x % 10
x = x // 10
ans = r * neg
if ans > (1<<31)-1 or ans < -(1<<31):
return 0
return ans
- 分析
这题看似简单,其实挖坑很多。需要考虑到以下几点:
- 末尾是0的整数翻转后要舍弃0,如10→1,120→21。因此我使用最常规的“对10取余,对10地板除”的方式来翻转整数,此处不宜采用强制类型转换的方式。
- 考虑正负两种情况。设一个符号的flag进行调整。
- Overflow的问题,不管是翻转前还是翻转后。如果是翻转前就溢出,就要考虑整型值的范围问题,由于python是动态语言,处理变量类型比较灵活,整型值为64位的范围,远远大于题设的32位。再考虑翻转后溢出的情况,利用移位运算求出范围,翻转之后再进行判断。
【LeetCode】#7 Reverse Integer的更多相关文章
- 【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...
- 【LeetCode】7. Reverse Integer 整型数反转
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:不 ...
- 【leetcode】7. Reverse Integer
题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思 ...
- 【LeetCode】007. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【一天一道LeetCode】#7. Reverse Integer
一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...
随机推荐
- PowerShell Start 1 - 使用Get - Help.
详细帮助命令参见:https://msdn.microsoft.com/zh-cn/powershell/scripting/getting-started/fundamental/getting-d ...
- REST WCF Service中的WebMessageBodyStyle
这个参数是个枚举包括如下值: WebMessageBodyStyle.Bare WebMessageBodyStyle.Wrapped WebMessageBodyStyle.WrappedReque ...
- [Solved]bcdedit.exe文件权限问题
最近在项目开发过程中,要使用到C:\Windows\system32\bcdedit.exe 但是在使用过程中,发现了一个问题.在命令行下面使用bcdedit.exe,如果是以管理员方式运行的命令行就 ...
- asp.net timer viewstate
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 一个ubuntu phper的自我修养(workbench)
workbench从此和navicat的激活码说再见 workbench是一个免费易用功能强大的mysql图形化管理软件,navicat上用到的功能,workbench上都能找到. 一.workben ...
- Angular--页面间切换及传值的四种方法
1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...
- 理解LSTM网络
转载自:http://blog.csdn.net/ycheng_sjtu/article/details/48792467
- linux vmware安装完成后如何设置桥接上网
linux 主机初步安装完成后还是不能上网,如何设置共享上网 1 首先要明白上网方式:虚拟机网卡-------------------vmnet1--------------------真实机网卡( ...
- .NET蓝牙开源库:32feet.NET
在用C#调用蓝牙编程一文中我留个小悬念就是:InTheHand.Net.Personal.dll是怎么来的?这篇文章来解答这个问题,InTheHand.Net.Personal.dll就是来源于今天要 ...
- The first DP!
P3399 丝绸之路 题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚. ...