[코테 풀이] Find N Unique Integers Sum up to Zero

시내·2024년 6월 28일

Q_1304) Find N Unique Integers Sum up to Zero

출처 : https://leetcode.com/problems/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.

class Solution {
    public int[] sumZero(int n) {
        int[] answer = new int[n];
        int index = 0;
        int num = 1000;
        int count = 0;
        if (n % 2 == 1) {
            answer[index++] = 0;
            count++;
        }
        while (count < n) {
            answer[index++] = num;
            answer[index++] = num * (-1);
            count += 2;
            num--;
        }
        return answer;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글