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

 

 

Leetcode【111】Find N Unique Integers Sum up to Zero(Python版)Leetcode【111】Find N Unique Integers Sum up to Zero(Python版) 请叫我算术嘉 发布了197 篇原创文章 · 获赞 69 · 访问量 152万+ 他的留言板 关注
上一篇:C# list中ConvertAll的使用


下一篇:【纸牌识别】基于matlab形态学扑克牌识别【含Matlab源码 1352期】