LeetCode-1304. Find N Unique Integers Sum up to Zero

网友投稿 238 2022-08-29

LeetCode-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 = 5Output: [-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 = 3Output: [-1,0,1]

Example 3:

Input: n = 1Output: [0]

Constraints:

​​1 <= n <= 1000​​

​​题解:​​

class Solution {public: vector sumZero(int n) { int sum = 0; vector res; if (n == 0) { return {0}; } for (int i = 1; i < n; i++) { sum += i; res.push_back(i); } res.push_back(-sum); return res; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-1488. Avoid Flood in The City
下一篇:打通数据孤岛,“无界式”数字营销来了!(数字化营销场景)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~