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

Commitf5c89a0

Browse files
refactor 239
1 parent97357b8 commitf5c89a0

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,26 @@ How about using a data structure such as deque (double-ended queue)?
3535
*/
3636
publicclass_239 {
3737

38-
publicint[]maxSlidingWindow(int[]nums,intk) {
39-
if (nums ==null ||nums.length ==0 ||k ==0) {
40-
returnnewint[0];
41-
}
42-
PriorityQueue<Integer>heap =newPriorityQueue<>((a,b) ->b -a);
43-
int[]res =newint[nums.length -k +1];
44-
for (inti =0;i <nums.length;i++) {
45-
if (i <k) {
46-
heap.offer(nums[i]);
47-
if (i ==k -1) {
48-
res[0] =heap.peek();
38+
publicstaticclassSolution1 {
39+
publicint[]maxSlidingWindow(int[]nums,intk) {
40+
if (nums ==null ||nums.length ==0 ||k ==0) {
41+
returnnewint[0];
42+
}
43+
PriorityQueue<Integer>heap =newPriorityQueue<>((a,b) ->b -a);
44+
int[]res =newint[nums.length -k +1];
45+
for (inti =0;i <nums.length;i++) {
46+
if (i <k) {
47+
heap.offer(nums[i]);
48+
if (i ==k -1) {
49+
res[0] =heap.peek();
50+
}
51+
}else {
52+
heap.remove(nums[i -k]);
53+
heap.offer(nums[i]);
54+
res[i -k +1] =heap.peek();
4955
}
50-
}else {
51-
heap.remove(nums[i -k]);
52-
heap.offer(nums[i]);
53-
res[i -k +1] =heap.peek();
5456
}
57+
returnres;
5558
}
56-
returnres;
5759
}
5860
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66
importorg.junit.BeforeClass;
77
importorg.junit.Test;
88

9-
/**
10-
* Created by fishercoder on 1/10/17.
11-
*/
129
publicclass_239Test {
13-
privatestatic_239test;
10+
privatestatic_239.Solution1solution1;
1411
privatestaticint[]expected;
1512
privatestaticint[]actual;
1613
privatestaticint[]nums;
1714
privatestaticintk;
1815

1916
@BeforeClass
2017
publicstaticvoidsetup() {
21-
test =new_239();
18+
solution1 =new_239.Solution1();
2219
}
2320

2421
@Before
@@ -35,7 +32,7 @@ public void test1() {
3532
nums =newint[]{1,3, -1, -3,5,3,6,7};
3633
k =3;
3734
expected =newint[]{3,3,5,5,6,7};
38-
actual =test.maxSlidingWindow(nums,k);
35+
actual =solution1.maxSlidingWindow(nums,k);
3936
Assert.assertArrayEquals(expected,actual);
4037

4138
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp