{"id":6018,"date":"2019-12-29T10:52:37","date_gmt":"2019-12-29T18:52:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6018"},"modified":"2019-12-29T10:53:44","modified_gmt":"2019-12-29T18:53:44","slug":"leetcode-1304-find-n-unique-integers-sum-up-to-zero","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/generation\/leetcode-1304-find-n-unique-integers-sum-up-to-zero\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1304. Find N Unique Integers Sum up to Zero"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>, return&nbsp;<strong>any<\/strong>&nbsp;array containing&nbsp;<code>n<\/code>&nbsp;<strong>unique<\/strong>&nbsp;integers such that they add up to 0.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5\n<strong>Output:<\/strong> [-7,-1,1,3,4]\n<strong>Explanation:<\/strong> These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3\n<strong>Output:<\/strong> [-1,0,1]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 1\n<strong>Output:<\/strong> [0]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Generation<\/strong><\/h2>\n\n\n\n<p>Use numbers from {-n\/2, &#8230; n\/2} + {0 if n is odd}<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> sumZero(int n) {\n    vector<int> ans;\n    for (int i = 1; i <= n \/ 2; ++i) {\n      ans.push_back(i);\n      ans.push_back(-i);\n    }\n    if (ans.size() != n) ans.push_back(0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n, return&nbsp;any&nbsp;array containing&nbsp;n&nbsp;unique&nbsp;integers such that they add up to 0. Example 1: Input: n = 5 Output: [-7,-1,1,3,4] Explanation: These arrays also are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[523],"tags":[20,222,524,376],"class_list":["post-6018","post","type-post","status-publish","format-standard","hentry","category-generation","tag-array","tag-easy","tag-generation","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6018"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6018\/revisions"}],"predecessor-version":[{"id":6020,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6018\/revisions\/6020"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}