-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0622
No.0622.Design Circular Queue
- Loading branch information
Showing
9 changed files
with
160 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 22 additions & 22 deletions
44
solution/0600-0699/0622.Design Circular Queue/Solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
struct MyCircularQueue { | ||
queue: Vec<i32>, | ||
left: usize, | ||
right: usize, | ||
q: Vec<i32>, | ||
size: usize, | ||
capacity: usize, | ||
front: usize, | ||
} | ||
|
||
/** | ||
* `&self` means the method takes an immutable reference. | ||
* If you need a mutable reference, change it to `&mut self` instead. | ||
*/ | ||
impl MyCircularQueue { | ||
fn new(k: i32) -> Self { | ||
let k = k as usize; | ||
Self { | ||
queue: vec![0; k], | ||
left: 0, | ||
right: 0, | ||
capacity: k, | ||
MyCircularQueue { | ||
q: vec![0; k as usize], | ||
size: 0, | ||
capacity: k as usize, | ||
front: 0, | ||
} | ||
} | ||
|
||
fn en_queue(&mut self, value: i32) -> bool { | ||
if self.is_full() { | ||
return false; | ||
} | ||
self.queue[self.right % self.capacity] = value; | ||
self.right += 1; | ||
let rear = (self.front + self.size) % self.capacity; | ||
self.q[rear] = value; | ||
self.size += 1; | ||
true | ||
} | ||
|
||
fn de_queue(&mut self) -> bool { | ||
if self.is_empty() { | ||
return false; | ||
} | ||
self.left += 1; | ||
self.front = (self.front + 1) % self.capacity; | ||
self.size -= 1; | ||
true | ||
} | ||
|
||
fn front(&self) -> i32 { | ||
if self.is_empty() { | ||
return -1; | ||
-1 | ||
} else { | ||
self.q[self.front] | ||
} | ||
self.queue[self.left % self.capacity] | ||
} | ||
|
||
fn rear(&self) -> i32 { | ||
if self.is_empty() { | ||
return -1; | ||
-1 | ||
} else { | ||
let rear = (self.front + self.size - 1) % self.capacity; | ||
self.q[rear] | ||
} | ||
self.queue[(self.right - 1) % self.capacity] | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
self.right - self.left == 0 | ||
self.size == 0 | ||
} | ||
|
||
fn is_full(&self) -> bool { | ||
self.right - self.left == self.capacity | ||
self.size == self.capacity | ||
} | ||
} |
Oops, something went wrong.