Skip to content

1304. Find N Unique Integers Sum up to Zero

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  vector<int> sumZero(int n) {
    vector<int> ans(n);

    for (int i = 0; i < n; ++i)
      ans[i] = i * 2 - n + 1;

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public int[] sumZero(int n) {
    int[] ans = new int[n];

    for (int i = 0; i < n; ++i)
      ans[i] = i * 2 - n + 1;

    return ans;
  }
}
1
2
3
class Solution:
  def sumZero(self, n: int) -> list[int]:
    return list(range(1 - n, n, 2))