-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedSquared.java
More file actions
49 lines (46 loc) · 1.3 KB
/
SortedSquared.java
File metadata and controls
49 lines (46 loc) · 1.3 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
import java.util.Scanner;
public class SortedSquared
{
public int[] sortedSquares(int[] nums)
{
int left = 0, right = 0;
int length = nums.length;
int[] NUM = new int[length];
left = 0;
right = length - 1;
while (left <= right)
{
if (((nums[left]) * (nums[left])) > ((nums[right]) * (nums[right])))
{
NUM[length - 1] = nums[left] * nums[left];
length--;
left++;
} else
{
NUM[length - 1] = nums[right] * nums[right];
length--;
right--;
}
}
return NUM;
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("输入数组长度:");
int length = scanner.nextInt();
int[] nums = new int[length];
System.out.println("请输入数组");
for (int i = 0; i < length; i++)
{
nums[i] = scanner.nextInt();
}
SortedSquared sort = new SortedSquared();
int[] NUM = sort.sortedSquares(nums);
System.out.println("新的平方和数组为");
for (int j = 0; j < length; j++)
{
System.out.println(NUM[j]);
}
}
}