[LeetCode] Find N Unique Integers Sum up to Zero

아르당·2026년 4월 18일

LeetCode

목록 보기
269/303
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

정수 n이 주어졌을 때, n개의 고유한 정수를 포함하고, 합이 0인 배열을 반환해라.

Example

#1
Input: n = 5
Output: [-7, -1, 1, 3, 4]
Explanation: [-5, -1, 1, 2, 3], [-3, -1, 2, -2, 4] 또한 받아들여진다.

#2
Input: n = 3
Output: [-1, 0, 1]

#3
Input: n = 1
Output: [0]

Constraints

  • 1 <= n <= 1000

Solved

class Solution {
    public int[] sumZero(int n) {
        int[] result = new int[n];
        result[0] = n * (1 - n) / 2;

        for(int i = 1; i < n; i++){
            result[i] = i;
        }

        return result;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글