Leetcode【111】Find N Unique Integers Sum up to Zero(Python版)

Pythonic 

class Solution:
    def sumZero(self, n: int) -> List[int]:
        return range(1-n, n, 2)

菜鸟版 

class Solution(object):
    def sumZero(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        ans = []
        if n % 2 == 0:
            tmp_1 = [i for i in range(1,n/2+1)]
            tmp_2 = [-i for i in range(1,n/2+1)]
            ans = tmp_1 + tmp_2
        else:
            tmp_1 = [i for i in range(1,(n-1)/2+1)]
            tmp_2 = [-i for i in range(1,(n-1)/2+1)]
            ans = tmp_1 + tmp_2 + ["0"]
        return ans
发布了197 篇原创文章 · 获赞 69 · 访问量 152万+

猜你喜欢

转载自blog.csdn.net/ssjdoudou/article/details/103935940