1304. Find N Unique Integers Sum up to Zero

Given an integer n, return any array containing n unique integers such that they add up to 0.

分奇偶吧,偶就给i和-i,奇的话就最后多给个0

class Solution(object):
    def sumZero(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        ans = []
        for i in range(1, n // 2 + 1, 1):
            ans.append(i)
            ans.append(-i)
        if n % 2 == 1:
            ans.append(0)
        return ans

 

posted @ 2020-06-29 12:36  whatyouthink  阅读(20)  评论(0编辑  收藏  举报