LeetCode-Python-7. 整数反转
2019-03-28 19:51
453 查看
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123 输出: 321
示例 2:
输入: -123 输出: -321
示例 3:
输入: 120 输出: 21
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
思路:
简单的字符串处理。去除符号后转字符串反转再转int即可,记得判断有没有溢出。
[code]class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ flag = 0 if x < 0: flag = 1 if flag: s = str(x)[1:] s = s[::-1] x = -1 *int(s) else: s = str(x) s = s[::-1] x = int(s) if x < -1 * 2 **31 or x > 2** 31 -1: return 0 return x
相关文章推荐
- LeetCode7.python实现:整数反转问题☆
- python实现leetcode-第七题-整数反转
- Leetcode7.整数反转(python3)
- LeetCode--Reverse Integer(整数反转)Python
- LeetCode题解(python)-7. 整数反转
- leetcode的python实现 刷题笔记7: 反转整数
- python入门习题——7,反转整数(简单)
- LeetCode 557. 反转字符串中的单词 III Python
- golang_算法: leetcode_字符串02-整数反转
- [leetcode, python] Reverse Words in a String 反转字符串
- LeetCode--First Missing Positive (第一个丢失的正整数)Python
- leetcode 7. Reverse Integer整数反转
- [置顶] [LeetCode] Reverse Integer 整数反转
- [Leetcode,python] Reverse String 反转字符串
- LeetCode - 7. 反转整数
- [LeetCode-7] Reverse Integer(反转整数)
- [leetcode, python] Reverse Words in a String 反转字符串
- LeetCode--Invert Binary Tree (反转二叉树)Python
- [leetcode]反转整数
- [leetcode, python] Reverse Words in a String 反转字符串