본문 바로가기
python-algorithm

leetcode 1304. Find N Unique Integers Sum up to Zero

by 무적김두칠 2024. 1. 10.

https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/description/

 

Find N Unique Integers Sum up to Zero - LeetCode

Can you solve this real interview question? 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.   Example 1: Input: n = 5 Output: [-7,-1,1,3,4] Explanation: These arrays als

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
class Solution:
    def sumZero(self, n: int-> List[int]:
        if n % 2 == 0:
            answer = []
        else:
            answer = [0]
        for i in range(1, n//2 + 1):
            answer.append(i)
            answer.append(-i)
        return answer
cs

n이 홀수, 짝수일때 구분해서 좌우 대칭으로 숫자 입력하면 될듯

반응형

댓글