Skip to content

Commit a5530bb

Browse files
add 977
1 parent 3be038e commit a5530bb

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_977.java) | O(nlogn) | O(1) | |Easy| Array
3233
|976|[Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_976.java) | O(nlogn) | O(1) | |Easy| Math Array
3334
|974|[Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-sums-divisible-by-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_974.java) | O(n) | O(n) | |Medium| Array|
3435
|973|[K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_973.java) | O(nlogn) | O(K) | |Easy| Math Sort|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 977. Squares of a Sorted Array
7+
*
8+
* Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.
9+
*
10+
* Example 1:
11+
* Input: [-4,-1,0,3,10]
12+
* Output: [0,1,9,16,100]
13+
*
14+
* Example 2:
15+
* Input: [-7,-3,2,3,11]
16+
* Output: [4,9,9,49,121]
17+
*
18+
*
19+
* Note:
20+
* 1 <= A.length <= 10000
21+
* -10000 <= A[i] <= 10000
22+
* A is sorted in non-decreasing order.
23+
*/
24+
public class _977 {
25+
public static class Solution1 {
26+
public int[] sortedSquares(int[] A) {
27+
int[] result = new int[A.length];
28+
for (int i = 0; i < A.length; i++) {
29+
result[i] = (int) Math.pow(A[i], 2);
30+
}
31+
Arrays.sort(result);
32+
return result;
33+
}
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._977;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _977Test {
10+
private static _977.Solution1 solution1;
11+
private static int[] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _977.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[] {-4, -1, 0, 3, 10};
21+
assertArrayEquals(new int[] {0, 1, 9, 16, 100}, solution1.sortedSquares(A));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
A = new int[] {-7, -3, 2, 3, 11};
27+
assertArrayEquals(new int[] {4, 9, 9, 49, 121}, solution1.sortedSquares(A));
28+
}
29+
}

0 commit comments

Comments
 (0)