structMyCircularQueue { 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. */ fnnew(k: i32) -> Self { letmut vec = Vec::with_capacity(k.clone() asusize); vec.resize(k asusize, 0); MyCircularQueue { v : vec, head : 0, tail : 0, count : 0 } }
/** Insert an element into the circular queue. Return true if the operation is successful. */ fnen_queue(&mutself, value: i32) -> bool { ifself.is_full() { returnfalse; }
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;
fnisParenBalanced(s : &String) -> bool { let (mut left, mut right) = (0, 0); for c in s.chars() { if c == '(' { left = left + 1; } elseif c == ')' { if left == 0 { returnfalse; } left = left - 1 } } return left == 0; } visited.insert(s.clone()); queue.push_back(s.clone());
letmut stop = false; while !queue.is_empty() { letstr = queue.pop_front().unwrap(); if isParenBalanced(&str) { result.push(str.clone()); stop = true; } if stop { continue; }
for it instr.char_indices() { if it.1 != '(' && it.1 != ')' { continue; } letmut next = str.clone(); next.remove(it.0); if visited.contains(&next) { continue; } queue.push_back(next.clone()); visited.insert(next.clone()); } }
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 += " "; }