Skip to content

Commit 31064d5

Browse files
add 622
1 parent e8a6150 commit 31064d5

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ _If you like this project, please leave me a star._ ★
591591
|625|[Minimum Factorization](https://leetcode.com/problems/minimum-factorization/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_625.java) | |Medium |
592592
|624|[Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_624.java) | |Easy | Sort, Array
593593
|623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_623.java) | |Medium | Tree
594+
|622|[Design Circular Queue](https://leetcode.com/problems/design-circular-queue/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_622.java) | |Medium | Design, Queue
594595
|621|[Task Scheduler](https://leetcode.com/problems/task-scheduler/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_621.java) | |Medium | Greedy, Queue
595596
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_617.java) | |Easy | Tree, Recursion
596597
|616|[Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_616.java) | | Medium | String
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _622 {
6+
public static class Solution1 {
7+
public static class MyCircularQueue {
8+
9+
int[] arr;
10+
int rearIndex;//this one points to the rear of the queue and could grow to 3000 which is the max calls that this problem is bound to
11+
int size;//this is the max size of this circule queue
12+
int frontIndex;
13+
14+
public MyCircularQueue(int k) {
15+
arr = new int[k];
16+
Arrays.fill(arr, -1);
17+
size = k;
18+
rearIndex = 0;
19+
frontIndex = 0;
20+
}
21+
22+
public boolean enQueue(int value) {
23+
if (arr[rearIndex % size] == -1) {
24+
arr[rearIndex % size] = value;
25+
rearIndex++;
26+
return true;
27+
} else {
28+
return false;
29+
}
30+
}
31+
32+
public boolean deQueue() {
33+
if (arr[frontIndex % size] != -1) {
34+
arr[frontIndex % size] = -1;
35+
frontIndex++;
36+
return true;
37+
} else {
38+
return false;
39+
}
40+
}
41+
42+
public int Front() {
43+
return arr[frontIndex % size];
44+
}
45+
46+
public int Rear() {
47+
return arr[(rearIndex - 1) % size];
48+
}
49+
50+
public boolean isEmpty() {
51+
return rearIndex == frontIndex;
52+
}
53+
54+
public boolean isFull() {
55+
return Math.abs(rearIndex - frontIndex) == size;
56+
}
57+
}
58+
59+
}
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._622;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class _622Test {
9+
private static _622.Solution1.MyCircularQueue myCircularQueue;
10+
11+
@Test
12+
public void test1() {
13+
myCircularQueue = new _622.Solution1.MyCircularQueue(3);
14+
assertEquals(true, myCircularQueue.enQueue(1));
15+
assertEquals(true, myCircularQueue.enQueue(2));
16+
assertEquals(true, myCircularQueue.enQueue(3));
17+
assertEquals(false, myCircularQueue.enQueue(4));
18+
assertEquals(3, myCircularQueue.Rear());
19+
assertEquals(true, myCircularQueue.isFull());
20+
assertEquals(true, myCircularQueue.deQueue());
21+
assertEquals(true, myCircularQueue.enQueue(4));
22+
assertEquals(4, myCircularQueue.Rear());
23+
}
24+
25+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy