-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquaresOfASortedArray.java
More file actions
55 lines (49 loc) · 1.48 KB
/
SquaresOfASortedArray.java
File metadata and controls
55 lines (49 loc) · 1.48 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
package leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* SquaresOfASortedArray
* https://leetcode-cn.com/problems/squares-of-a-sorted-array/
* 1. 题目不难, 直接能想到的思路: 全部计算后排序
* 2. 双指针同时移动(归并排序, 逆向), 应该会比本实现快些
*
* @since 2020-10-16
*/
public class SquaresOfASortedArray {
public static void main(String[] args) {
SquaresOfASortedArray sol = new SquaresOfASortedArray();
int[] res = sol.sortedSquares(new int[]{-7, -3, 2, 3, 11});
for (int r : res) {
System.out.println(r);
}
}
public int[] sortedSquares(int[] A) {
int sepIdx = 0;
for (sepIdx = 0; sepIdx < A.length; sepIdx++) {
if (A[sepIdx] >= 0) {
break;
}
}
List<Integer> resList = new LinkedList<>();
for (int i = sepIdx; i < A.length; i++) {
resList.add(A[i] * A[i]);
}
int lastIdx = 0;
for (int j = sepIdx - 1; j >= 0; j--) {
int t = A[j] * A[j];
while (lastIdx < resList.size()) {
if (t < resList.get(lastIdx)) {
break;
} else {
lastIdx++;
}
}
resList.add(lastIdx, t);
}
int[] res = new int[resList.size()];
for (int i = 0; i < res.length; i++) {
res[i] = resList.get(i);
}
return res;
}
}