-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumZero.java
More file actions
56 lines (48 loc) · 1.23 KB
/
SumZero.java
File metadata and controls
56 lines (48 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/****
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
Runtime: 76 ms, faster than 8.37% of JavaScript online submissions for Find N Unique Integers Sum up to Zero.
Memory Usage: 35.1 MB, less than 100.00% of JavaScript online submissions for Find N Unique Integers Sum up to Zero.
****/
/**
* @param {number} n
* @return {number[]}
*/
var sumZero = function(n) {
if(n===1) {
return [0];
}
else {
var out = new Array(n);
var count = 1;
if(n%2==0) {
for(i=0; i<n; i+=2) {
out[i] = count;
out[i+1] = parseInt('-'+count.toString(), 10);
count ++;
}
// 1, -1, 2, -2 ...
}
else {
out[0] = 0;
for(i=1; i<n; i+=2) {
out[i] = i;
out[i+1] = parseInt('-'+i.toString(), 10);
count ++;
}
// 0, 1, -1, 2, -2...
}
return out;
}
};