Skip to content

Commit 66611fb

Browse files
add 1304
1 parent 7f5990a commit 66611fb

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy||
1112
|1302|[Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1302.java) | |Medium||
1213
|1300|[Sum of Mutated Array Closest to Target](https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1300.java) | |Medium||
1314
|1299|[Replace Elements with Greatest Element on Right Side](https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1299.java) | |Easy||
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1304. Find N Unique Integers Sum up to Zero
5+
*
6+
* Given an integer n, return any array containing n unique integers such that they add up to 0.
7+
*
8+
* Example 1:
9+
* Input: n = 5
10+
* Output: [-7,-1,1,3,4]
11+
* Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].
12+
*
13+
* Example 2:
14+
* Input: n = 3
15+
* Output: [-1,0,1]
16+
*
17+
* Example 3:
18+
* Input: n = 1
19+
* Output: [0]
20+
* */
21+
public class _1304 {
22+
public static class Solution1 {
23+
public int[] sumZero(int n) {
24+
int[] result = new int[n];
25+
int start = -n / 2;
26+
for (int i = 0; i < n / 2; i++) {
27+
result[i] = start++;
28+
}
29+
if (n % 2 == 0) {
30+
start++;
31+
}
32+
for (int i = n / 2; i < n; i++) {
33+
result[i] = start++;
34+
}
35+
return result;
36+
}
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1304;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _1304Test {
9+
private static _1304.Solution1 solution1;
10+
11+
@BeforeClass
12+
public static void setup() {
13+
solution1 = new _1304.Solution1();
14+
}
15+
16+
@Test
17+
public void test1() {
18+
CommonUtils.printArray(solution1.sumZero(5));
19+
}
20+
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy