欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python 实现反转整数的案例(很容易懂的那种)

时间:2021-09-25 08:17:55|栏目:Python代码|点击:

题目:

给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

示例 1:

  输入: 123

  输出: 321   

示例 2:

  输入: -123

  输出: -321   

示例 3:

  输入: 120

  输出: 21

注意:

假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 。请根据这个假设,如果反转后整数溢出那么就返回 0。

解题思路:

1.实现数据的反转

如果是正数:

tra = 0
while x != 0:
  n2 = x%10
  x = x //10
  tra = tra*10 + n2

如果是负数就abs()一下这个数

2.溢出判定

给出范围[−2^31, 2^31 − 1]

则输出的结果tra就必须满足这个范围.

代码:

class Solution(object):
 def reverse(self, x):
 base = 1
 for i in range(31):
 base = base * 2
 two_Max = base - 1
 two_Min = -base
 tra = 0
 if x < 0:
 x = abs(x)
 while x != 0:
 n2 = x % 10
 if tra > abs(two_Min) // 10 or (tra == abs(two_Min) // 10 and n2 < -8):
  return 0
 x = x // 10
 tra = tra * 10 + n2
 return -tra
 else:
 while x != 0:
 n2 = x % 10
 if tra > two_Max//10 or (tra == two_Max and n2 > 7 ):
  return 0
 x = x // 10
 tra = tra * 10 + n2
 return tra

补充:python实现数字反转_python 数字怎么反转

每次写 Python 都会忘记该怎么写,最后只能去 Stack Overflow 查?我也一样。时间一长,这让人厌倦。

这15个 Python 技巧和窍门,可以帮你提高效率

1. 交换值

x, y = 1, 2 
print(x, y) 
x, y = y, x 
print(x, y)

2. 字符串列表合并为一个字符串

sentence_list = ["my", "name", "is", "George"] 
sentence_string = " ".join(sentence_list) 
print(sentence_string)

3. 将字符串拆分为子字符串列表

sentence_string = "my name is George" 
sentence_string.split() 
print(sentence_string)

4. 通过数字填充初始化列表

[0]*1000 
# List of 1000 
zeros [8.2]*1000 
# List of 1000 8.2's

5. 字典合并

x = {'a': 1, 'b': 2} 
y = {'b': 3, 'c': 4} 
z = {**x, **y}

6. 反转字符串

name = "George" name[::-1] 

7. 从函数返回多个值

def get_a_string(): 
a = "George" 
b = "is" 
c = "cool" return a, b, c 
sentence = get_a_string() (a, b, c) = sentence

8. 列表解析式

a = [1, 2, 3] 
b = [num*2 for num in a] # Create a new list by multiplying each element in a by 2

9. 遍历字典

m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} for key, value in m.items(): print('{0}: {1}'.format(key, value))

10. 同时遍历列表的索引和值

m = ['a', 'b', 'c', 'd'] for index, value in enumerate(m): print('{0}: {1}'.format(index, value))

11. 初始化空容器

a_list = list() 
a_dict = dict() 
a_map = map() 
a_set = set()

12. 删除字符串两端的无用字符

name = " George " name_2 = "George///" name.strip() # prints "George" name_2.strip("/") # prints "George"

13. 列表中出现最多的元素

test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] print(max(set(test), key = test.count))

14. 检查对象的内存使用情况

import sys x = 1 print(sys.getsizeof(x))

15. 将 dict 转换为 XML

from xml.etree.ElementTree import Element def dict_to_xml(tag, d): ''' Turn a simple dict of key/value pairs into XML ''' elem = Element(tag) for key, val in d.items(): child = Element(key) child.text = str(val) elem.append(child) return elem

上一篇:浅析Python中的getattr(),setattr(),delattr(),hasattr()

栏    目:Python代码

下一篇:使用python实现下载我们想听的歌曲,速度超快

本文标题:Python 实现反转整数的案例(很容易懂的那种)

本文地址:http://www.codeinn.net/misctech/177262.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有