PHP前端开发

python怎么将整数反转输出

百变鹏仔 3个月前 (01-22) #Python
文章标签 整数

给定一个32位的符号整数,返回它的反转整数

Example 1: Input: 123Output: 321
Example 2: Input: -123Output: -321
Example 3: Input: 120Output: 21

假设该整数的大小范围为:,如果反转整数溢出,就返回0。

1:正常整数方法实现,利用余数*10累加的方法完成。需要注意的是,python对整数除法采用“向下取整”机制,所以正数和负数要区别运算。

def reverse(self, x):        """        :type x: int        :rtype: int        """        num = 0        if x == 0:            return 0        if x pow(2,31)-1 or num <p>2:整数转字符串,反转字符串,然后再转整数</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><pre class="brush:php;toolbar:false">def reverse(self, x):        """        :type x: int        :rtype: int        """        plus_minus = ""        reverse_x = ""        if xpow(2,31)-1 or int(reverse_x)<pow><p>相关教程推荐:<a href="http://www.php.cn/course/list/30/type/2.html" target="_self">Python视频教程</a></p></pow>