提交 4247a05a 编写于 作者: 梦境迷离's avatar 梦境迷离

rust leetcode

上级 32dabe35
......@@ -304,17 +304,65 @@ impl Solution {
* 从尾到头打印链表
```rust
impl Solution {
pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
let mut ret = Vec::<i32>::new();
let mut node = head.as_ref();
loop {
if let Some(root) = node {
ret.push(root.val);
node = root.next.as_ref();
} else { break }
pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
let mut ret = Vec::<i32>::new();
let mut node = head.as_ref();
loop {
if let Some(root) = node {
ret.push(root.val);
node = root.next.as_ref();
} else { break }
}
ret.reverse();
ret
}
}
```
* 二叉搜索树的范围和
```rust
impl Solution {
pub fn range_sum_bst(root: Option<Rc<RefCell<TreeNode>>>, l: i32, r: i32) -> i32 {
let mut ret = 0;
let mut nodes = VecDeque::new();
nodes.push_back(root);
while !nodes.is_empty() {
let tmp = nodes.pop_back();
if let Some(node) = tmp {
if let Some(n) = node {
if n.try_borrow().unwrap().val >= l && n.try_borrow().unwrap().val <= r {
ret += n.try_borrow().unwrap().val
}
//满足条件继续查找
if n.try_borrow().unwrap().val > l {
nodes.push_back(n.try_borrow().unwrap().left.clone());
}
if n.try_borrow().unwrap().val < r {
nodes.push_back(n.try_borrow().unwrap().right.clone());
}
}
}
ret.reverse();
ret
}
ret
}
pub fn range_sum_bst2(root: Option<Rc<RefCell<TreeNode>>>, l: i32, r: i32) -> i32 {
let mut ret = 0;
fn bst(root: Option<Rc<RefCell<TreeNode>>>, l: i32, r: i32, ret: &mut i32) {
if let Some(node) = root {
if node.try_borrow().unwrap().val >= l && node.try_borrow().unwrap().val <= r {
*ret += node.try_borrow().unwrap().val
}
if node.try_borrow().unwrap().val > l {
bst(node.try_borrow().unwrap().left.clone(), l, r, ret)
}
if node.try_borrow().unwrap().val < r {
bst(node.try_borrow().unwrap().right.clone(), l, r, ret)
}
}
}
//可变借用,修改外函数的变量ret
bst(root, l, r, &mut ret);
ret
}
}
```
......@@ -488,7 +488,7 @@ fn interview_06() {
if let Some(root) = node {
ret.push(root.val);
node = root.next.as_ref();
} else { break }
} else { break; }
}
ret.reverse();
ret
......@@ -501,6 +501,58 @@ fn interview_06() {
println!("{:?}", ret);
}
///二叉搜索树的范围和
fn leetcode_938() {
println!("leetcode_938");
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn range_sum_bst(root: Option<Rc<RefCell<TreeNode>>>, l: i32, r: i32) -> i32 {
let mut ret = 0;
let mut nodes = VecDeque::new();
nodes.push_back(root);
while !nodes.is_empty() {
let tmp = nodes.pop_back();
if let Some(node) = tmp {
if let Some(n) = node {
if n.try_borrow().unwrap().val >= l && n.try_borrow().unwrap().val <= r {
ret += n.try_borrow().unwrap().val
}
//满足条件继续查找
if n.try_borrow().unwrap().val > l {
nodes.push_back(n.try_borrow().unwrap().left.clone());
}
if n.try_borrow().unwrap().val < r {
nodes.push_back(n.try_borrow().unwrap().right.clone());
}
}
}
}
ret
}
pub fn range_sum_bst2(root: Option<Rc<RefCell<TreeNode>>>, l: i32, r: i32) -> i32 {
let mut ret = 0;
fn bst(root: Option<Rc<RefCell<TreeNode>>>, l: i32, r: i32, ret: &mut i32) {
if let Some(node) = root {
if node.try_borrow().unwrap().val >= l && node.try_borrow().unwrap().val <= r {
*ret += node.try_borrow().unwrap().val
}
if node.try_borrow().unwrap().val > l {
bst(node.try_borrow().unwrap().left.clone(), l, r, ret)
}
if node.try_borrow().unwrap().val < r {
bst(node.try_borrow().unwrap().right.clone(), l, r, ret)
}
}
}
//可变借用,修改外函数的变量ret
bst(root, l, r, &mut ret);
ret
}
}
}
///所有方法调用
pub fn solutions() {
interview_58_2();
......@@ -518,6 +570,7 @@ pub fn solutions() {
interview_05();
leetcode_1221();
interview_06();
leetcode_938();
}
fn print_vec(nums: Vec<i32>) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册