【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, ...
随机推荐
- 在DOM中搜索元素
方法 现代浏览器中使用XPath document.getElementById document/node.getElementsByTagName Limit search by parent e ...
- WebService错误:使用 XSL 样式表无法查看 XML 输入
在浏览器中输入URL: 'http://localhost/test.aspx' 出现下面错误提示信息: 无法显示 XML 页. 使用 XSL 样式表无法查看 XML 输入.请更正错误然后单击 刷新 ...
- 《C++ API设计》作者Martin Reddy访谈问题征集
Martin Reddy博士是软件行业的一名老兵,有着15年以上的从业经验,共撰写过40多篇论文,拥有3项软件专利,并与他人合著了Level of Detail for 3D Graphics.另外, ...
- phpcms V9 常用函数 及 代码整理
常用函数 及 常用代码 总结如下 <?php //转换字符串或者数组的编码 str_charset($in_charset, $out_charset, $str_or_arr) //获取菜单 ...
- Makefile编译库
funs.h: #ifndef __FUNS_H__ #define __FUNS_H__ void fun1(); #endif funs.c #include "funs.h" ...
- 应用ERP系统与企业的关系
随着ERP系统的深入发展,越来越多的企业开始实施ERP,ERP实施是借用一种新的管理模式来改造原企业旧的管理模式,是先进的.行之有效的管理思想和方法.ERP软件在实际的推广应用中,其应用深度和广度 ...
- CountDownLatch和CyclicBarrier 举例详解
有时候会有这样的需求,多个线程同时工作,然后其中几个可以随意并发执行,但有一个线程需要等其他线程工作结束后,才能开始.举个例子,开启多个线程分块下载一个大文件,每个线程只下载固定的一截,最后由另外一个 ...
- EF 如何更新少量字段
EF更新少量字段需要解决两个问题 1.动态的将需要更新的字段提取出来 2.将提取出来的字段设为更新状态 通常更新的时候,都是根据条件将实体取出来,然后赋值字段,最后更新整个实体,所以在方法上看似是更新 ...
- AtCoder Grand Contest 008 A
Problem Statement Snuke has a calculator. It has a display and two buttons. Initially, the display s ...
- 详解Linux目录(目录树详细解释)
给大家一篇关于Linux目录 方面的详细说明,好好读一下! Linux目录详解(RHEL5.4) linux有四种基本文件系统类型:--普通文件:如文本文件.c语言源代码.shell脚本等,可以用ca ...