Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1efa219

Browse files
add a solution for 977
1 parent91b52fd commit1efa219

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

‎src/main/java/com/fishercoder/solutions/_977.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,35 @@
44

55
publicclass_977 {
66
publicstaticclassSolution1 {
7-
publicint[]sortedSquares(int[]A) {
8-
int[]result =newint[A.length];
9-
for (inti =0;i <A.length;i++) {
10-
result[i] = (int)Math.pow(A[i],2);
7+
/**
8+
* O(nlogn) solution
9+
*/
10+
publicint[]sortedSquares(int[]nums) {
11+
int[]result =newint[nums.length];
12+
for (inti =0;i <nums.length;i++) {
13+
result[i] = (int)Math.pow(nums[i],2);
1114
}
1215
Arrays.sort(result);
1316
returnresult;
1417
}
1518
}
19+
20+
publicstaticclassSolution2 {
21+
/**
22+
* O(n) solution
23+
*/
24+
publicint[]sortedSquares(int[]nums) {
25+
int[]ans =newint[nums.length];
26+
for (inti =nums.length -1,left =0,right =nums.length -1;i <nums.length &&left <=right;i--) {
27+
if (Math.abs(nums[left]) <Math.abs(nums[right])) {
28+
ans[i] =nums[right] *nums[right];
29+
right--;
30+
}else {
31+
ans[i] =nums[left] *nums[left];
32+
left++;
33+
}
34+
}
35+
returnans;
36+
}
37+
}
1638
}

‎src/test/java/com/fishercoder/_977Test.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77
importstaticorg.junit.Assert.assertArrayEquals;
88

99
publicclass_977Test {
10-
privatestatic_977.Solution1solution1;
11-
privatestaticint[]A;
10+
privatestatic_977.Solution1solution1;
11+
privatestatic_977.Solution2solution2;
12+
privatestaticint[]A;
1213

13-
@BeforeClass
14-
publicstaticvoidsetup() {
15-
solution1 =new_977.Solution1();
16-
}
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_977.Solution1();
17+
solution2 =new_977.Solution2();
18+
}
1719

18-
@Test
19-
publicvoidtest1() {
20-
A =newint[] {-4, -1,0,3,10};
21-
assertArrayEquals(newint[] {0,1,9,16,100},solution1.sortedSquares(A));
22-
}
20+
@Test
21+
publicvoidtest1() {
22+
A =newint[]{-4, -1,0,3,10};
23+
assertArrayEquals(newint[]{0,1,9,16,100},solution1.sortedSquares(A));
24+
assertArrayEquals(newint[]{0,1,9,16,100},solution2.sortedSquares(A));
25+
}
2326

24-
@Test
25-
publicvoidtest2() {
26-
A =newint[] {-7, -3,2,3,11};
27-
assertArrayEquals(newint[] {4,9,9,49,121},solution1.sortedSquares(A));
28-
}
27+
@Test
28+
publicvoidtest2() {
29+
A =newint[]{-7, -3,2,3,11};
30+
assertArrayEquals(newint[]{4,9,9,49,121},solution1.sortedSquares(A));
31+
assertArrayEquals(newint[]{4,9,9,49,121},solution2.sortedSquares(A));
32+
}
2933
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp