【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, ...
随机推荐
- iOS 里面 Swift与Objective-C混编,Swift与C++混编的一些比较
即使你尽量用Swift编写iOS程序,难免会遇到部分算法是用C++语言编写的.那你只能去问问”度娘“或“狗哥”怎么用Swift调用C++算法. 一,C,C++, Objective-C,S ...
- ADB server didn't ACK
当我们通过eclipse开发Android应用时,会连接真机会使用模拟器进行仿真,有时候启动失败,会提示这样的错误. 工具/原料 Eclipse CMD命令窗口 方法/步骤 首先通过CMD启动adb服 ...
- 干货——myeclipse快捷键
好了废话不多说.先上快捷键快捷键 alt alt+ 返回操作记录的上一步 alt+ 返回操作记录的下一步 alt+shift+o 同一 ...
- [已解决] protobuf Missing input file
如果proto描述文件在当前目录,要以"./"开始 如: protoc ./test.proto --java_out=./ 文章来源:http://www.cnblogs.com ...
- 在oneAPM参加第一个项目小结
从12月15日开始加入进入oneAPM的第二个项目,也是我真正近距离接触项目的一次吧,到今天差不多接近尾声了,很高心能和大家一起共同改造这个项目,虽然说我做的贡献并不大,但是身临项目真的会收获很多体会 ...
- 写入文件(txt格式)
#region 写入文件 /// <summary> /// 写入文件 /// </summary> /// <param ...
- Centos7搭建需要mysql的网站
1.在centos7上安装好http.php.php-mysql服务 php-mysql是用来链接的工具 2.在centos5上yum安装mysql 注意在搭建本地yum源时把校验关闭,不然安装不上 ...
- AtCoder Grand Contest 008 A
Problem Statement Snuke has a calculator. It has a display and two buttons. Initially, the display s ...
- Android开发工具类
7种无须编程的DIY开发工具 你知道几个? 现如今,各种DIY开发工具不断的出现,使得企业和个人在短短几分钟内就能完成应用的创建和发布,大大节省了在时间和资金上的投入.此外,DIY工 具的出现,也帮助 ...
- Windows DOS 窗口设置字体颜色
2015-04-21 10:54:43 #include <windows.h> #include <iostream> using namespace std; #defin ...