1374.生成每种字符都是奇数个的字符串、622.设计循环队列

This commit is contained in:
游由 2022-08-02 12:30:16 +08:00
parent 92ec0441b5
commit 966e70608d
3 changed files with 145 additions and 15 deletions

View File

@ -1,36 +1,118 @@
struct MyCircularQueue {}
/// [622.设计循环队列](https://leetcode-cn.com/problems/design-circular-queue/)
///
/// 2022-08-02 10:24:43
///
/// 设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO先进先出原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
///
/// 循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
///
/// + 你的实现应该支持如下操作:
/// * `MyCircularQueue(k)`: 构造器,设置队列长度为 k 。
/// * `Front`: 从队首获取元素。如果队列为空,返回 -1 。
/// * `Rear`: 获取队尾元素。如果队列为空,返回 -1 。
/// * `enQueue(value)`: 向循环队列插入一个元素。如果成功插入则返回真。
/// * `deQueue()`: 从循环队列中删除一个元素。如果成功删除则返回真。
/// * `isEmpty()`: 检查循环队列是否为空。
/// * `isFull()`: 检查循环队列是否已满。
/// + **示例:**
/// ```c++
/// MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
/// circularQueue.enQueue(1); // 返回 true
/// circularQueue.enQueue(2); // 返回 true
/// circularQueue.enQueue(3); // 返回 true
/// circularQueue.enQueue(4); // 返回 false队列已满
/// circularQueue.Rear(); // 返回 3
/// circularQueue.isFull(); // 返回 true
/// circularQueue.deQueue(); // 返回 true
/// circularQueue.enQueue(4); // 返回 true
/// circularQueue.Rear(); // 返回 4
/// ```
/// + **提示:**
/// * 所有的值都在 0 至 1000 的范围内;
/// * 操作数将在 1 至 1000 的范围内;
/// * 请不要使用内置的队列库。
/// + Related Topics
/// * 设计
/// * 队列
/// * 数组
/// * 链表
/// * 👍 337
/// * 👎 0
///
/// `&self` 表示该方法采用不可变引用。
/// 如果您需要可变引用,请将其更改为 `&mut self`。
struct MyCircularQueue {
s:usize,
e:usize,
c:usize,
q:Vec<i32>
}
/**
* `&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 {
unimplemented!()
MyCircularQueue {
s: 0,
e: 0,
c: (k + 1) as usize,
q: vec![-1; (k + 1) as usize],
}
}
fn en_queue(&self, value: i32) -> bool {
unimplemented!()
fn en_queue(&mut self, value: i32) -> bool {
if self.is_full() {
return false;
}
self.q[self.e] = value;
self.e = (self.e + 1) % self.c;
true
}
fn de_queue(&self) -> bool {
unimplemented!()
fn de_queue(&mut self) -> bool {
if self.is_empty() {
return false;
}
self.s = (self.s + 1) % self.c;
true
}
fn front(&self) -> i32 {
unimplemented!()
match self.is_empty() {
true => -1,
_ => self.q[self.s]
}
}
fn rear(&self) -> i32 {
unimplemented!()
match self.is_empty() {
true => -1,
_ => self.q[(self.e + self.c - 1) % self.c]
}
}
fn is_empty(&self) -> bool {
unimplemented!()
self.s == self.e
}
fn is_full(&self) -> bool {
unimplemented!()
((self.e + 1) % self.c) == self.s
}
}
#[cfg(test)]
mod test {
use crate::design::q0622::MyCircularQueue;
#[test]
fn test_q0622() {
let mut obj = MyCircularQueue::new(3);
assert_eq!(obj.en_queue(1), true);
assert_eq!(obj.en_queue(2), true);
assert_eq!(obj.en_queue(3), true);
assert_eq!(obj.en_queue(4), false);
assert_eq!(obj.rear(), 3);
assert_eq!(obj.is_full(), true);
assert_eq!(obj.de_queue(), true);
assert_eq!(obj.en_queue(4), true);
assert_eq!(obj.rear(), 4);
}
}

View File

@ -173,6 +173,7 @@ mod q1202;
mod q1218;
mod q1269;
mod q1310;
mod q1374;
mod q1447;
mod q1473;
mod q1482;

View File

@ -0,0 +1,47 @@
use crate::Solution;
impl Solution {
/// [1374.生成每种字符都是奇数个的字符串](https://leetcode-cn.com/problems/generate-a-string-with-characters-that-have-odd-counts/)
///
/// 2022-08-02 09:42:05
///
/// 给你一个整数 `n`,请你返回一个含 _`n`_ 个字符的字符串,其中每种字符在该字符串中都恰好出现 **奇数次** _**。**_
///
/// 返回的字符串必须只含小写英文字母。如果存在多个满足题目要求的字符串,则返回其中任意一个即可。
///
/// + **示例 1**
/// - **输入:** n = 4
/// - **输出:** "pppz"
/// - **解释:** "pppz" 是一个满足题目要求的字符串,因为 'p' 出现 3 次,且 'z' 出现 1 次。当然,还有很多其他字符串也满足题目要求,比如:"ohhh" 和 "love"。
/// + **示例 2**
/// - **输入:** n = 2
/// - **输出:** "xy"
/// - **解释:** "xy" 是一个满足题目要求的字符串,因为 'x' 和 'y' 各出现 1 次。当然,还有很多其他字符串也满足题目要求,比如:"ag" 和 "ur"。
/// + **示例 3**
/// - **输入:** n = 7
/// - **输出:** "holasss"
/// + **提示:**
/// * `1 <= n <= 500`
/// + Related Topics
/// * 字符串
/// * 👍 64
/// * 👎 0
pub fn generate_the_string(n: i32) -> String {
if (n & 1) == 1 {
String::from_utf8(vec![b'a'; n as usize]).unwrap()
} else {
String::from_utf8(vec![b'a'; (n - 1) as usize]).unwrap() + "b"
}
}
}
#[cfg(test)]
mod test {
use crate::Solution;
#[test]
fn test_q1374() {
assert_eq!(Solution::generate_the_string(4), "aaab".to_owned());
}
}