【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, ...
随机推荐
- 初始化datetimepicker的时候就报了js异常
全栈眼中的http这一章分别从前端视角和后端视角来分析前后端所关注的侧重点.前端可以通过抓包工具或者chrome devtools 查看每个请求,同域下的资源请求数量等来找出优化点,更关注的是一个页面 ...
- HttpGet 请求(带参数)
package com.example.util; import java.io.BufferedReader;import java.io.IOException;import java.io.In ...
- 终于搞定office 2013中文双引号无法匹配问题啦!!!
设计>>字体>>自定义字体>>所有字体改为宋体>>保存>>点击字体确认当前字体是自己刚新建的>>点击旁边设为默认值>> ...
- C语言-Hello, world
你好, 世界 --1-- 语言的编写准备 1.1 C语言源文件的编译执行过程 1.2 常见文件的拓展名 1.3 常用的命令行指令 1.4 环境及运行方法 --2--编写代码 2.1练习 --3-- ...
- Java 基础 Map 练习题
第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...
- 一个jQ版大图滚动
难得周末能休息,也是越发的代码难受,手就想敲点东西,这不闲着无聊敲了一个Jq版的大图滚动,不足之处大家批评指正: 运作环境win7,代码编辑器是:sublime; 我把源码复制了一下, <!do ...
- easyui DataGrid 工具类之 util js
var jq; var tab; var tabsIndex; /** ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
- Appium 关于如何获取 apk 包名即 appPackage 和 appActivity
方法2: a.启动待测apk b.开启日志输出:adb logcat>D:/log.txt c.关闭日志输出:ctrl+c d.查看日志 进入通过adb 连接上设备以后,通过如下命令查看包名 C ...
- dmidecode查看设备硬件信息
在bash里输入:dmidecode -s system-product-name 或者lshw -class system 在Linux系统环境下(CentOS .4和Ubuntu .04已确认), ...