LeetCode 622 Design Circular Queue

Problem Statement

use std::vec::Vec;

struct MyCircularQueue {
v : Vec<i32>,
head : usize,
tail : usize,
count : usize
}

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MyCircularQueue {

/** Initialize your data structure here. Set the size of the queue to be k. */
fn new(k: i32) -> Self {
let mut vec = Vec::with_capacity(k.clone() as usize);
vec.resize(k as usize, 0);
MyCircularQueue {
v : vec,
head : 0, tail : 0, count : 0
}
}

/** Insert an element into the circular queue. Return true if the operation is successful. */
fn en_queue(&mut self, value: i32) -> bool {
if self.is_full() {
return false;
}

self.v[self.tail.clone()] = value;
self.tail = (self.tail + 1) % self.v.len();
self.count += 1;
return true;
}

/** Delete an element from the circular queue. Return true if the operation is successful. */
fn de_queue(&mut self) -> bool {
if self.is_empty() {
return false;
}
self.v[self.head.clone()] = 0;
self.head = (self.head + 1) % self.v.len();
self.count -= 1;
return true;
}

/** Get the front item from the queue. */
fn front(&self) -> i32 {
if self.is_empty() {
return - 1;
}
return self.v[self.head.clone()];
}

/** Get the last item from the queue. */
fn rear(&self) -> i32 {
if self.is_empty() {
return -1;
}
return self.v[(self.tail - 1 + self.v.len()) % self.v.len()];
}

/** Checks whether the circular queue is empty or not. */
fn is_empty(&self) -> bool {
return self.count == 0;
}

/** Checks whether the circular queue is full or not. */
fn is_full(&self) -> bool {
return self.count == self.v.len();
}
}

LeetCode 301 Remove Invalid Parentheses

Problem Statement

First attempt on implementing solutions for leetcode problems using Rust. For someone like me with a C++ background,
coding in Rust requires a paradigm change. Move semantics are everywhere, everything has to be explicit, and there are
really not so many similarities of standard libraries between Rust and C++. In any cases, it’s fun to code in Rust,
and I am sure this code can be more Rustified.

use std::collections::HashSet;
use std::collections::VecDeque;

impl Solution {
pub fn remove_invalid_parentheses(s: String) -> Vec<String> {
let mut result = Vec::new();
let mut visited = HashSet::new();
let mut queue = VecDeque::new();

fn isParenBalanced(s : &String) -> bool {
let (mut left, mut right) = (0, 0);
for c in s.chars() {
if c == '(' {
left = left + 1;
} else if c == ')' {
if left == 0 {
return false;
}
left = left - 1
}
}
return left == 0;
}
visited.insert(s.clone());
queue.push_back(s.clone());

let mut stop = false;
while !queue.is_empty() {
let str = queue.pop_front().unwrap();
if isParenBalanced(&str) {
result.push(str.clone());
stop = true;
}
if stop {
continue;
}

for it in str.char_indices() {
if it.1 != '(' && it.1 != ')' {
continue;
}
let mut next = str.clone();
next.remove(it.0);
if visited.contains(&next) {
continue;
}
queue.push_back(next.clone());
visited.insert(next.clone());
}
}

return result;
}
}

SRM 748 DIV II 500


class Yllion {
vector<string> getString(string input) {
vector<string> ans;
stringstream s(input);
string str;
while(s >> str) ans.push_back(str);
return ans;
}
public:
string getPower(string a, string b) {
unordered_map<string, int> s2i;
s2i["one"] = 0; s2i["ten"] = 1;
s2i["hundred"] = 2;
s2i["myriad"] = 4; s2i["myllion"] = 8;
s2i["byllion"] = 16; s2i["tryllion"] = 32;
s2i["quadryllion"] = 64; s2i["quintyllion"] = 128;
s2i["sextyllion"] = 256; s2i["septyllion"] = 512;
s2i["octyllion"] = 1024; s2i["nonyllion"] = 2048;
s2i["decyllion"] = 4096;
unordered_map<int, string> i2s;
i2s[0] = "one"; i2s[1] = "ten";
i2s[2] = "hundred";
i2s[4] = "myriad"; i2s[8] = "myllion";
i2s[16] = "byllion"; i2s[32] = "tryllion";
i2s[64] = "quadryllion"; i2s[128] = "quintyllion";
i2s[256] = "sextyllion"; i2s[512] = "septyllion";
i2s[1024] = "octyllion"; i2s[2048] = "nonyllion";
i2s[4096] = "decyllion";

auto va = getString(a), vb = getString(b);
int c = 0;
for (auto s : va) {
c += s2i[s];
}
for (auto s : vb) {
c += s2i[s];
}

vector<string> ans;
for (int i = 12; i >= 0; --i) {
int base = pow(2, i);
if (c < base) continue;
ans.push_back(i2s[base]);
if (base == 1) break;
c = c % base;
}

if (!c) ans.push_back("one");

reverse(ans.begin(), ans.end());
string ret;
for (int i = 0; i < ans.size(); ++i) {
ret += ans[i];
if (i != ans.size() - 1) ret += " ";
}

return ret;
}
}