mirror of https://github.com/doocs/leetcode.git
feat: add solutions to lc problems: No.0217,0232,0380
- No.0217.Contains Duplicate - No.0232.Implement Queue using Stacks - No.0380.Insert Delete GetRandom O(1)
This commit is contained in:
parent
b4c1b2151a
commit
2de0a9d328
|
|
@ -41,6 +41,14 @@
|
|||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
**方法一:排序**
|
||||
|
||||
排序数组,然后两个相邻元素是否相同即可。
|
||||
|
||||
**方法二:哈希表**
|
||||
|
||||
遍历元素并记录,当第二次出现时,直接返回 `true`。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
|
@ -135,6 +143,32 @@ var containsDuplicate = function (nums) {
|
|||
};
|
||||
```
|
||||
|
||||
### **Rust**
|
||||
|
||||
```rust
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
|
||||
nums.iter().collect::<HashSet<&i32>>().len() != nums.len()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn contains_duplicate(mut nums: Vec<i32>) -> bool {
|
||||
nums.sort();
|
||||
let n = nums.len();
|
||||
for i in 1..n {
|
||||
if nums[i - 1] == nums[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -117,6 +117,32 @@ var containsDuplicate = function (nums) {
|
|||
};
|
||||
```
|
||||
|
||||
### **Rust**
|
||||
|
||||
```rust
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
|
||||
nums.iter().collect::<HashSet<&i32>>().len() != nums.len()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn contains_duplicate(mut nums: Vec<i32>) -> bool {
|
||||
nums.sort();
|
||||
let n = nums.len();
|
||||
for i in 1..n {
|
||||
if nums[i - 1] == nums[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
|
||||
nums.iter().collect::<HashSet<&i32>>().len() != nums.len()
|
||||
}
|
||||
}
|
||||
|
|
@ -237,6 +237,70 @@ class MyQueue {
|
|||
*/
|
||||
```
|
||||
|
||||
### **Rust**
|
||||
|
||||
```rust
|
||||
struct MyQueue {
|
||||
in_stack: Vec<i32>,
|
||||
out_stack: Vec<i32>,
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl MyQueue {
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
in_stack: vec![],
|
||||
out_stack: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, x: i32) {
|
||||
self.in_stack.push(x);
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> i32 {
|
||||
if self.out_stack.is_empty() {
|
||||
self.fill_out();
|
||||
}
|
||||
self.out_stack.pop().unwrap()
|
||||
}
|
||||
|
||||
fn peek(&mut self) -> i32 {
|
||||
if self.out_stack.is_empty() {
|
||||
self.fill_out();
|
||||
}
|
||||
*self.out_stack.last().unwrap()
|
||||
}
|
||||
|
||||
fn empty(&self) -> bool {
|
||||
self.in_stack.is_empty() && self.out_stack.is_empty()
|
||||
}
|
||||
|
||||
fn fill_out(&mut self){
|
||||
let MyQueue { in_stack, out_stack } = self;
|
||||
if out_stack.is_empty() {
|
||||
while !in_stack.is_empty() {
|
||||
out_stack.push(in_stack.pop().unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your MyQueue object will be instantiated and called as such:
|
||||
* let obj = MyQueue::new();
|
||||
* obj.push(x);
|
||||
* let ret_2: i32 = obj.pop();
|
||||
* let ret_3: i32 = obj.peek();
|
||||
* let ret_4: bool = obj.empty();
|
||||
*/
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -219,6 +219,70 @@ class MyQueue {
|
|||
*/
|
||||
```
|
||||
|
||||
### **Rust**
|
||||
|
||||
```rust
|
||||
struct MyQueue {
|
||||
in_stack: Vec<i32>,
|
||||
out_stack: Vec<i32>,
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl MyQueue {
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
in_stack: vec![],
|
||||
out_stack: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, x: i32) {
|
||||
self.in_stack.push(x);
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> i32 {
|
||||
if self.out_stack.is_empty() {
|
||||
self.fill_out();
|
||||
}
|
||||
self.out_stack.pop().unwrap()
|
||||
}
|
||||
|
||||
fn peek(&mut self) -> i32 {
|
||||
if self.out_stack.is_empty() {
|
||||
self.fill_out();
|
||||
}
|
||||
*self.out_stack.last().unwrap()
|
||||
}
|
||||
|
||||
fn empty(&self) -> bool {
|
||||
self.in_stack.is_empty() && self.out_stack.is_empty()
|
||||
}
|
||||
|
||||
fn fill_out(&mut self){
|
||||
let MyQueue { in_stack, out_stack } = self;
|
||||
if out_stack.is_empty() {
|
||||
while !in_stack.is_empty() {
|
||||
out_stack.push(in_stack.pop().unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your MyQueue object will be instantiated and called as such:
|
||||
* let obj = MyQueue::new();
|
||||
* obj.push(x);
|
||||
* let ret_2: i32 = obj.pop();
|
||||
* let ret_3: i32 = obj.peek();
|
||||
* let ret_4: bool = obj.empty();
|
||||
*/
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
struct MyQueue {
|
||||
in_stack: Vec<i32>,
|
||||
out_stack: Vec<i32>,
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl MyQueue {
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
in_stack: vec![],
|
||||
out_stack: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, x: i32) {
|
||||
self.in_stack.push(x);
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> i32 {
|
||||
if self.out_stack.is_empty() {
|
||||
self.fill_out();
|
||||
}
|
||||
self.out_stack.pop().unwrap()
|
||||
}
|
||||
|
||||
fn peek(&mut self) -> i32 {
|
||||
if self.out_stack.is_empty() {
|
||||
self.fill_out();
|
||||
}
|
||||
*self.out_stack.last().unwrap()
|
||||
}
|
||||
|
||||
fn empty(&self) -> bool {
|
||||
self.in_stack.is_empty() && self.out_stack.is_empty()
|
||||
}
|
||||
|
||||
fn fill_out(&mut self){
|
||||
let MyQueue { in_stack, out_stack } = self;
|
||||
if out_stack.is_empty() {
|
||||
while !in_stack.is_empty() {
|
||||
out_stack.push(in_stack.pop().unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your MyQueue object will be instantiated and called as such:
|
||||
* let obj = MyQueue::new();
|
||||
* obj.push(x);
|
||||
* let ret_2: i32 = obj.pop();
|
||||
* let ret_3: i32 = obj.peek();
|
||||
* let ret_4: bool = obj.empty();
|
||||
*/
|
||||
|
|
@ -132,7 +132,7 @@ class RandomizedSet {
|
|||
public RandomizedSet() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean insert(int val) {
|
||||
if (m.containsKey(val)) {
|
||||
return false;
|
||||
|
|
@ -141,7 +141,7 @@ class RandomizedSet {
|
|||
l.add(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public boolean remove(int val) {
|
||||
if (!m.containsKey(val)) {
|
||||
return false;
|
||||
|
|
@ -153,7 +153,7 @@ class RandomizedSet {
|
|||
m.remove(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public int getRandom() {
|
||||
int idx = rnd.nextInt(l.size());
|
||||
return l.get(idx);
|
||||
|
|
@ -180,14 +180,14 @@ public:
|
|||
RandomizedSet() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool insert(int val) {
|
||||
if (mp.count(val)) return false;
|
||||
mp[val] = nums.size();
|
||||
nums.push_back(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool remove(int val) {
|
||||
if (!mp.count(val)) return false;
|
||||
int idx = mp[val];
|
||||
|
|
@ -197,7 +197,7 @@ public:
|
|||
nums.pop_back();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int getRandom() {
|
||||
return nums[rand() % nums.size()];
|
||||
}
|
||||
|
|
@ -258,6 +258,105 @@ func (this *RandomizedSet) GetRandom() int {
|
|||
*/
|
||||
```
|
||||
|
||||
### **TypeScript**
|
||||
|
||||
```ts
|
||||
class RandomizedSet {
|
||||
public map: Map<number, number>;
|
||||
public arr: number[];
|
||||
public index: number;
|
||||
|
||||
constructor() {
|
||||
this.map = new Map();
|
||||
this.arr = new Array(2 * 10 ** 5).fill(0);
|
||||
this.index = -1;
|
||||
}
|
||||
|
||||
insert(val: number): boolean {
|
||||
const { map, arr } = this;
|
||||
if (map.has(val)) {
|
||||
return false;
|
||||
}
|
||||
this.index++;
|
||||
arr[this.index] = val;
|
||||
map.set(val, this.index);
|
||||
return true;
|
||||
}
|
||||
|
||||
remove(val: number): boolean {
|
||||
const { arr, map, index } = this;
|
||||
if (!map.has(val)) {
|
||||
return false;
|
||||
}
|
||||
const i = map.get(val);
|
||||
[arr[i], arr[index]] = [arr[index], arr[i]];
|
||||
map.set(arr[i], i);
|
||||
map.delete(arr[index]);
|
||||
this.index--;
|
||||
return true;
|
||||
}
|
||||
|
||||
getRandom(): number {
|
||||
const i = Math.floor(Math.random() * (this.index + 1));
|
||||
return this.arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your RandomizedSet object will be instantiated and called as such:
|
||||
* var obj = new RandomizedSet()
|
||||
* var param_1 = obj.insert(val)
|
||||
* var param_2 = obj.remove(val)
|
||||
* var param_3 = obj.getRandom()
|
||||
*/
|
||||
```
|
||||
|
||||
### **Rust**
|
||||
|
||||
```rust
|
||||
use std::collections::HashSet;
|
||||
use rand::Rng;
|
||||
|
||||
struct RandomizedSet {
|
||||
list: HashSet<i32>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl RandomizedSet {
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
list: HashSet::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, val: i32) -> bool {
|
||||
self.list.insert(val)
|
||||
}
|
||||
|
||||
fn remove(&mut self, val: i32) -> bool {
|
||||
self.list.remove(&val)
|
||||
}
|
||||
|
||||
fn get_random(&self) -> i32 {
|
||||
let i = rand::thread_rng().gen_range(0, self.list.len());
|
||||
*self.list.iter().collect::<Vec<&i32>>()[i]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your RandomizedSet object will be instantiated and called as such:
|
||||
* let obj = RandomizedSet::new();
|
||||
* let ret_1: bool = obj.insert(val);
|
||||
* let ret_2: bool = obj.remove(val);
|
||||
* let ret_3: i32 = obj.get_random();
|
||||
*/
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ class RandomizedSet {
|
|||
public RandomizedSet() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean insert(int val) {
|
||||
if (m.containsKey(val)) {
|
||||
return false;
|
||||
|
|
@ -106,7 +106,7 @@ class RandomizedSet {
|
|||
l.add(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public boolean remove(int val) {
|
||||
if (!m.containsKey(val)) {
|
||||
return false;
|
||||
|
|
@ -118,7 +118,7 @@ class RandomizedSet {
|
|||
m.remove(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public int getRandom() {
|
||||
int idx = rnd.nextInt(l.size());
|
||||
return l.get(idx);
|
||||
|
|
@ -145,14 +145,14 @@ public:
|
|||
RandomizedSet() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool insert(int val) {
|
||||
if (mp.count(val)) return false;
|
||||
mp[val] = nums.size();
|
||||
nums.push_back(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool remove(int val) {
|
||||
if (!mp.count(val)) return false;
|
||||
int idx = mp[val];
|
||||
|
|
@ -162,7 +162,7 @@ public:
|
|||
nums.pop_back();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int getRandom() {
|
||||
return nums[rand() % nums.size()];
|
||||
}
|
||||
|
|
@ -223,6 +223,105 @@ func (this *RandomizedSet) GetRandom() int {
|
|||
*/
|
||||
```
|
||||
|
||||
### **TypeScript**
|
||||
|
||||
```ts
|
||||
class RandomizedSet {
|
||||
public map: Map<number, number>;
|
||||
public arr: number[];
|
||||
public index: number;
|
||||
|
||||
constructor() {
|
||||
this.map = new Map();
|
||||
this.arr = new Array(2 * 10 ** 5).fill(0);
|
||||
this.index = -1;
|
||||
}
|
||||
|
||||
insert(val: number): boolean {
|
||||
const { map, arr } = this;
|
||||
if (map.has(val)) {
|
||||
return false;
|
||||
}
|
||||
this.index++;
|
||||
arr[this.index] = val;
|
||||
map.set(val, this.index);
|
||||
return true;
|
||||
}
|
||||
|
||||
remove(val: number): boolean {
|
||||
const { arr, map, index } = this;
|
||||
if (!map.has(val)) {
|
||||
return false;
|
||||
}
|
||||
const i = map.get(val);
|
||||
[arr[i], arr[index]] = [arr[index], arr[i]];
|
||||
map.set(arr[i], i);
|
||||
map.delete(arr[index]);
|
||||
this.index--;
|
||||
return true;
|
||||
}
|
||||
|
||||
getRandom(): number {
|
||||
const i = Math.floor(Math.random() * (this.index + 1));
|
||||
return this.arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your RandomizedSet object will be instantiated and called as such:
|
||||
* var obj = new RandomizedSet()
|
||||
* var param_1 = obj.insert(val)
|
||||
* var param_2 = obj.remove(val)
|
||||
* var param_3 = obj.getRandom()
|
||||
*/
|
||||
```
|
||||
|
||||
### **Rust**
|
||||
|
||||
```rust
|
||||
use std::collections::HashSet;
|
||||
use rand::Rng;
|
||||
|
||||
struct RandomizedSet {
|
||||
list: HashSet<i32>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl RandomizedSet {
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
list: HashSet::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, val: i32) -> bool {
|
||||
self.list.insert(val)
|
||||
}
|
||||
|
||||
fn remove(&mut self, val: i32) -> bool {
|
||||
self.list.remove(&val)
|
||||
}
|
||||
|
||||
fn get_random(&self) -> i32 {
|
||||
let i = rand::thread_rng().gen_range(0, self.list.len());
|
||||
*self.list.iter().collect::<Vec<&i32>>()[i]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your RandomizedSet object will be instantiated and called as such:
|
||||
* let obj = RandomizedSet::new();
|
||||
* let ret_1: bool = obj.insert(val);
|
||||
* let ret_2: bool = obj.remove(val);
|
||||
* let ret_3: i32 = obj.get_random();
|
||||
*/
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
use std::collections::HashSet;
|
||||
use rand::Rng;
|
||||
|
||||
struct RandomizedSet {
|
||||
list: HashSet<i32>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* `&self` means the method takes an immutable reference.
|
||||
* If you need a mutable reference, change it to `&mut self` instead.
|
||||
*/
|
||||
impl RandomizedSet {
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
list: HashSet::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, val: i32) -> bool {
|
||||
self.list.insert(val)
|
||||
}
|
||||
|
||||
fn remove(&mut self, val: i32) -> bool {
|
||||
self.list.remove(&val)
|
||||
}
|
||||
|
||||
fn get_random(&self) -> i32 {
|
||||
let i = rand::thread_rng().gen_range(0, self.list.len());
|
||||
*self.list.iter().collect::<Vec<&i32>>()[i]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your RandomizedSet object will be instantiated and called as such:
|
||||
* let obj = RandomizedSet::new();
|
||||
* let ret_1: bool = obj.insert(val);
|
||||
* let ret_2: bool = obj.remove(val);
|
||||
* let ret_3: i32 = obj.get_random();
|
||||
*/
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
class RandomizedSet {
|
||||
public map: Map<number, number>;
|
||||
public arr: number[];
|
||||
public index: number;
|
||||
|
||||
constructor() {
|
||||
this.map = new Map();
|
||||
this.arr = new Array(2 * 10 ** 5).fill(0);
|
||||
this.index = -1;
|
||||
}
|
||||
|
||||
insert(val: number): boolean {
|
||||
const { map, arr } = this;
|
||||
if (map.has(val)) {
|
||||
return false;
|
||||
}
|
||||
this.index++;
|
||||
arr[this.index] = val;
|
||||
map.set(val, this.index);
|
||||
return true;
|
||||
}
|
||||
|
||||
remove(val: number): boolean {
|
||||
const { arr, map, index } = this;
|
||||
if (!map.has(val)) {
|
||||
return false;
|
||||
}
|
||||
const i = map.get(val);
|
||||
[arr[i], arr[index]] = [arr[index], arr[i]];
|
||||
map.set(arr[i], i);
|
||||
map.delete(arr[index]);
|
||||
this.index--;
|
||||
return true;
|
||||
}
|
||||
|
||||
getRandom(): number {
|
||||
const i = Math.floor(Math.random() * (this.index + 1));
|
||||
return this.arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your RandomizedSet object will be instantiated and called as such:
|
||||
* var obj = new RandomizedSet()
|
||||
* var param_1 = obj.insert(val)
|
||||
* var param_2 = obj.remove(val)
|
||||
* var param_3 = obj.getRandom()
|
||||
*/
|
||||
Loading…
Reference in New Issue