难度等级简单

# 第一题(1)
'''
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个  整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的yuansu ''' class Solution(object): def two_sum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ for i,num in enumerate(nums): value = target - num if value in nums[i+1:]: return [i, nums[i+1:].index(value)+i+1] return None

自己错误的解法,忽视去掉一个元素后,其相应的下标也变了,代码如下

class Solution(object):
    def two_sum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for num1 in nums:
            result = []
            num1_i = nums.index(num1)
            result.append(num1_i)
            nums.remove(num1)
            for num2 in nums:
                if num1 +  num2 == target:
                    num2_i = nums.index(num2)
                    result.append(num2_i)
                    return result
        return None

 

第二题(7.整数反转

给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231,  231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0

做这个题前,要先知道怎么反转字符串,此博客(https://www.cnblogs.com/taceywong/p/8045127.html)列出好几种反转字符串的方法

结题代码:

法一:

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        #1 将x转换成字符串并反转,若有符号,一并反转了 
        reverse_str = ''.join(str(x)[::-1])    
        #2 若x为负数时,去掉负号,并将其转换成int*-1输出
        if '-' in reverse_str:
            res = int(reverse_str[:-1])*-1
        else:        
       res
= int(reverse_str) # 判断是否溢出 if res > (2**31-1) or res < (-2)**31: return 0 return res

法二:

思路:(来自LeetCode  id为 ‘灵魂画师牧码’的分析),主要是判断溢出部分(leetcode官方判断溢出的思路也是如此)

本题如果不考虑溢出问题,是非常简单的。解决溢出问题有两个思路,第一个思路是通过字符串转换加try catch的方式来解决,第二个思路就是通过数学计算来解决。
由于字符串转换的效率较低且使用较多库函数,所以解题方案不考虑该方法,而是通过数学计算来解决。
通过循环将数字x的每一位拆开,在计算新值时每一步都判断是否溢出。
溢出条件有两个,一个是大于整数最大值MAX_VALUE,另一个是小于整数最小值MIN_VALUE,设当前计算结果为ans,下一位为pop。
从ans * 10 + pop > MAX_VALUE这个溢出条件来看
当出现 ans > MAX_VALUE / 10 且 还有pop需要添加 时,则一定溢出
当出现 ans == MAX_VALUE / 10 且 pop > 7 时,则一定溢出,7是2^31 - 1的个位数
从ans * 10 + pop < MIN_VALUE这个溢出条件来看
当出现 ans < MIN_VALUE / 10 且 还有pop需要添加 时,则一定溢出
当出现 ans == MAX_VALUE / 10 且 pop < -8 时,则一定溢出,8是-2^31的个位数

 以下是自己根据此思路整的python代码,有点问题(当为负数时直接返回的是0,得不到正确的结果)

代码如下:

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        max_value = 2**31-1
        min_value = -2**31
        res = 0
        while x != 0:
            pop = x % 10
            if res > max_value / 10 and pop != 0 or res == max_value / 10 and pop > 7:
                return 0
            elif res < min_value / 10 and pop != 0 or res < min_value / 10 and pop < (-8):
                return 0
            res = res*10 + x % 10
            x /= 10
        return res

 

第三题(9)

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

法一:将整数转为字符串(简单粗暴)

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
     # 将x反转为字符串
reverse_str = str(x)[::-1]
     # 判断x是否为负数,负数的话反转后不能转为int类型
if x < 0 or x % 10 ==0 and x != o: return False else: reverse_int = int(reverse_str) if reverse_int == x: return True return False

法二(leetcode官方思路)

反转int数字的一半,效率高点

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0 or x % 10 == 0 and x != 0:return False
        res = 0
        while x > res:
            x, reverted_num = x // 10, x % 10
            res = res * 10 + reverted_num
     # 若x的位数为奇数时,通过res//10 去掉中间的一位数
return x == res or x == res // 10

 

 

 

posted @ 2019-06-20 16:46  一y样  阅读(379)  评论(0编辑  收藏  举报