Leetcode 7. Reverse Integer(python)

该题比较简单,但是这道题有点问题。。。。

python中的整数运算没有没有限制,但是在该oj系统中要求对大数输出0(题目并没有说明,但是在test case中可以看出)

于是偷了个懒,,,

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
    	s=‘‘
    	if x==0: return 0
    	if x<0: s+=‘-‘
    	x=abs(x)
    	while x!=0:
    		s+=str(x%10)
    		x=x/10
    	i=int(s)
    	if i>2147483647 or i<-2147483647:
    	    return 0
    	return i

  

时间: 2025-01-05 13:25:16

Leetcode 7. Reverse Integer(python)的相关文章

Leetcode 7. Reverse Integer(水)

7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could on

【leetcode】Reverse Integer(middle)☆

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出的方法 ①用数据类型转换long  或 long long ②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好) ③整体恢复,看数字是否相等. 思路:注意30000这样以0结尾的数字,注意越界时返回0. 我检查越界是通过把翻转的数字再翻转回去,看是否相等. int rever

leetcode——7 Reverse Integer(附有溢出整数翻转的实现)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer'

LeetCode 7 Reverse Integer(反转数字)

题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 其实这道题看起来非常简单,要实现也是几行代码的事.但是有个小问题容易被忽略,就是边界问题.什么意思呢?如果我们输入的整数超出了int的表达范围,这个问题要怎么解决呢? 用比int更大的数据类型存储我们转

LeetCode 7 Reverse Integer(翻转整数)

翻译 翻转一个整型数 例1:x = 123, 返回 321 例2:x = -123, 返回 -321 原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? (来自LeetCode官网) Here are some good questions to ask before coding. Bonus poi

leetcode 7 Reverse Integer(水题)

so easy,注意一下输入不爆int但是反转以后可能爆int. class Solution { public: int gao(int w){ if(w==0) return 1; else{ int ans=1; while(w--){ ans*=10; } return ans; } } int reverse(int x) { vector<int>v; if(x>=0){ int t=x; while(t>0){ v.push_back(t%10); t/=10; }

Leetcode 13. Roman to Integer(python)

class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ l=len(s) r,i=0,0 roman={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'CM':900,'CD':400,'XC':90,'XL':40,'IX':9,'IV':4} while i<l: if i&

【leetcode】Reverse Bits(middle)

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). 思路: n一直右移, ans一直左移. class So

leetcode 39. 组合总和(python)

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选取. 说明: 所有数字(包括 target)都是正整数.解集不能包含重复的组合. 示例 1: 输入: candidates = [2,3,6,7], target = 7,所求解集为:[ [7], [2,2,3]]示例 2: 输入: candidates = [2,3,5], target = 8,