-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSortedSquares.java
51 lines (47 loc) · 1.17 KB
/
SortedSquares.java
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
package ds;
import java.util.Arrays;
/**
* 有序数组的平方
* <p>
* <p>
* 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。
* <p>
* <p>
* <p>
* 示例 1:
* <p>
* 输入:[-4,-1,0,3,10]
* 输出:[0,1,9,16,100]
* 示例 2:
* <p>
* 输入:[-7,-3,2,3,11]
* 输出:[4,9,9,49,121]
* <p>
* <p>
* 提示:
* <p>
* 1 <= A.length <= 10000
* -10000 <= A[i] <= 10000
* A 已按非递减顺序排序。
*
* @author yangyi 2019年02月16日23:17:48
*/
public class SortedSquares {
public int[] sortedSquares(int[] A) {
if (A == null || A.length == 0) {
return new int[]{};
}
for (int i = 0; i < A.length; i++) {
A[i] *= A[i];
}
Arrays.sort(A);
return A;
}
public static void main(String[] args) {
int[] a = {-4, -1, 0, 3, 10};
int[] b = {-7, -3, 2, 3, 11};
SortedSquares sortedSquares = new SortedSquares();
System.out.println(Arrays.toString(sortedSquares.sortedSquares(a)));
System.out.println(Arrays.toString(sortedSquares.sortedSquares(b)));
}
}