rust
rust
冰糖style
当你的才华还撑不起你的野心的时候,就应该静下心来学习!
展开
-
Rust——并查集
Rust——并查集实现实现#![allow(unused)]//! 并查集:解决节点连接/关联问题/// 并查集 traittrait UF { fn is_connected(&mut self, p: usize, q: usize) -> bool; fn union_elements(&mut self, p: usize, q: usize); fn get_size(&self) -> usize;}/// 并查集结构原创 2021-07-15 12:53:31 · 221 阅读 · 0 评论 -
Rust——leetcode 677. 键值映射
use std::collections::HashMap;use std::rc::Rc;use std::cell::RefCell;// 节点定义struct Node {value: i32,next: HashMap<char, Rc<RefCell<Node>>>,}impl Node {pub fn new() -> Self {return Self {value: 0,next: HashMap::new(),};.原创 2021-07-14 17:15:25 · 129 阅读 · 0 评论 -
Rust——leetcode 211. 添加与搜索单词 - 数据结构设计
use std::collections::HashMap;use std::rc::Rc;use std::cell::RefCell;// 节点定义struct Node { is_word: bool, next: HashMap<char, Rc<RefCell<Node>>>,}impl Node { pub fn new_with_is_word(is_word: bool) -> Self { r原创 2021-07-14 17:11:50 · 109 阅读 · 0 评论 -
Rust_leetcode 208. 实现 Trie (前缀树)
字典树用于字符串处理代码实现#![allow(unused)]use std::collections::HashMap;use std::cell::RefCell;use std::rc::Rc;// 节点定义struct Node { is_word: bool, next: HashMap<char, Rc<RefCell<Node>>>,}impl Node { pub fn new_with_is_word(i原创 2021-07-14 17:09:45 · 452 阅读 · 0 评论 -
Rust——leetcode_307. 区域和检索 - 数组可修改
使用线段树来解决可修改的数组区域检索问题,时间复杂度为O(logn)struct NumArray { tree: SegmentTree}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */impl NumArray { fn new(nums:原创 2021-07-14 12:06:28 · 131 阅读 · 0 评论 -
Rust——leetcode_303. 区域和检索 - 数组不可变
struct NumArray { sum: Vec<i32>}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */impl NumArray { fn new(nums: Vec<i32>) -> Self {原创 2021-07-14 12:03:43 · 73 阅读 · 0 评论 -
Rust——线段树
Rust——线段树代码实现线段树是满二叉树,None代替没有元素的叶子节点线段树用于解决区间查询问题,所以不关心元素的增加删除操作。简单实现线段树的结构,所以这里没有写到修改线段树的元素查询时间复杂度O(logn),直接更新子节点的时间复杂度为O(n)代码实现#![allow(unused)]//! 线段树// 线段区间元素的融合方式trait 定义及其实现trait Merger { fn merge(&self, l: i32, r: i32) -> i32;原创 2021-07-14 10:09:38 · 197 阅读 · 0 评论 -
Rust——leetcode_剑指 Offer 40. 最小的k个数
使用最大堆的优先队列,解决最小的k个数问题最大堆优先队列先存储前k个元素,遍历剩下的元素,如果元素比队首元素小,就入队,否则忽略最中队列存储了最小的k个数impl Solution { pub fn get_least_numbers(arr: Vec<i32>, k: i32) -> Vec<i32> { let mut queue = PriorityQueue::new_priority(); for i in 0..k .原创 2021-07-08 12:25:21 · 122 阅读 · 0 评论 -
Rust——最大堆
Rust——最大堆实现排序测试实现#![allow(unused)]pub struct MaxHeap<E: PartialOrd + Copy> { data: Vec<E>}impl<E: PartialOrd + Copy> MaxHeap<E> { pub fn new_max_heap() -> MaxHeap<E> { Self { data: Vec::ne原创 2021-07-08 10:41:06 · 247 阅读 · 0 评论 -
Rust——二分查找法
实现一:循环fn search<E: PartialOrd>(data: Vec<E>, target: E) -> i32 { let mut l: i32 = 0; // 左闭右开区间 let mut r = data.len() as i32; while l < r { let mid = l + (r - l) / 2; if data[mid as usize] == target {原创 2021-06-27 13:47:14 · 717 阅读 · 0 评论