---
# System prepended metadata

title: 1304. Find N Unique Integers Sum up to Zero
tags: [Leetcode, Easy, Microsoft]

---

# 1304. Find N Unique Integers Sum up to Zero
###### tags: `Leetcode` `Microsoft` `Easy`
Link: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/
## Code
### 思路一
```java=
class Solution {
    public int[] sumZero(int n) {
        int[] arr = new int[n];
        int idx = 0;
        int sum = 0;
        for(int i = 1;i <= n-1;i++){
            arr[idx++] = i;
            sum += i;
        }
        arr[n-1] = 0-sum;
        return arr;
    }
}
```
### 思路二
```java=
class Solution {
    public int[] sumZero(int n) {
        int[] arr = new int[n];
        int idx = 0;
        if(n%2==1) arr[idx++] = 0;
        for(int i = 1;i <= n/2;i++){
            arr[idx++] = i;
            arr[idx++] = -i;
        }
        return arr;
    }
}
```
