Skip to content

Commit 5fda424

Browse files
author
Dream
committed
[update] 977
1 parent fbeaa35 commit 5fda424

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
|980|[Unique Paths III](https://leetcode.com/problems/unique-paths-iii/) | |Hard|
3333
|979|[Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/) | |Medium|
3434
|978|[Longest Turbulent Subarray](https://leetcode.com/problems/longest-turbulent-subarray/) | |Medium|
35-
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | |Easy|
35+
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [java_dream](./algorithms/squaresOfASortedArray/SquaresOfASortedArray.java) |Easy|
3636
|976|[Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/) | |Easy|
3737
|971|[Flip Binary Tree To Match Preorder Traversal](https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/) | |Medium|
3838
|969|[Pancake Sorting](https://leetcode.com/problems/pancake-sorting/) | |Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @author Dream
3+
*/
4+
public class SquaresOfASortedArray {
5+
public int[] sortedSquares(int[] nums) {
6+
int[] result = new int[nums.length];
7+
int q = nums.length - 1;
8+
for (int i = 0, j = nums.length - 1; i <= j;) {
9+
int iNum = nums[i] * nums[i];
10+
int jNum = nums[j] * nums[j];
11+
if (iNum > jNum) {
12+
result[q] = iNum;
13+
i++;
14+
q--;
15+
}else if (iNum < jNum){
16+
result[q] = jNum;
17+
j--;
18+
q--;
19+
}else {
20+
if (i == j) {
21+
result[q] = iNum;
22+
i++;
23+
}else {
24+
result[q] = iNum;
25+
i++;
26+
q--;
27+
result[q] = jNum;
28+
j--;
29+
q--;
30+
}
31+
}
32+
}
33+
return result;
34+
}
35+
}

0 commit comments

Comments
 (0)