1304. Find N Unique Integers Sum up to Zero

Posted wentiliangkaihua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1304. 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 also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].

Example 2:

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

Example 3:

Input: n = 1
Output: [0]

 

Constraints:

  • 1 <= n <= 1000

地铁老爷爷看手机.jpg

class Solution {
    public int[] sumZero(int n) {
        int[] res = new int[n];
        int m = n;
        int ind = 0;
        if(n % 2 != 0){
            res[ind++] = 0;
        }

        for(;ind < n; ){
            res[ind++] = m;
            res[ind++] = -m;
            m--;
        }
        return res;
    }
}

以上是关于1304. Find N Unique Integers Sum up to Zero的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode --- 1304. Find N Unique Integers Sum up to Zero 解题报告

Leetcode1304. Find N Unique Integers Sum up to Zero

Leetcode 1304. Find N Unique Integers Sum up to Zero

Find one unique integer

C++,带有 vector<unique_ptr> 的 find_if 无法正常工作

bzoj 1304: [CQOI2009]叶子的染色