Skip to content

Commit 2388bc1

Browse files
committed
feat: add solutions to lc problems: No.0045,0513,0611
- No.0045.Jump Game II - No.0513.Find Bottom Left Tree Value - No.0611.Valid Triangle Number
1 parent ab9eb42 commit 2388bc1

File tree

10 files changed

+411
-3
lines changed

10 files changed

+411
-3
lines changed

solution/0000-0099/0045.Jump Game II/README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
贪心。
48+
**方法一:动态规划**
49+
50+
**方法二:贪心**
4951

5052
<!-- tabs:start -->
5153

@@ -151,6 +153,46 @@ public class Solution {
151153
}
152154
```
153155

156+
### **C**
157+
158+
```c
159+
#define min(a, b) a < b ? a : b
160+
int jump(int* nums, int numsSize){
161+
int dp[numsSize];
162+
for (int i = 0; i < numsSize; i++) {
163+
dp[i] = numsSize;
164+
}
165+
dp[0] = 0;
166+
for (int i = 0; i < numsSize - 1; i++) {
167+
for (int j = i + 1; j < (min(i + nums[i] + 1, numsSize)); j++) {
168+
dp[j] = min(dp[j], dp[i] + 1);
169+
}
170+
}
171+
return dp[numsSize - 1];
172+
}
173+
```
174+
175+
### **Rust**
176+
177+
```rust
178+
impl Solution {
179+
pub fn jump(nums: Vec<i32>) -> i32 {
180+
let n = nums.len();
181+
let mut dp = vec![i32::MAX; n];
182+
dp[0] = 0;
183+
for i in 0..n - 1 {
184+
for j in 1..=nums[i] as usize {
185+
if i + j >= n {
186+
break;
187+
}
188+
dp[i + j] = dp[i + j].min(dp[i] + 1);
189+
}
190+
}
191+
dp[n - 1]
192+
}
193+
}
194+
```
195+
154196
### **...**
155197

156198
```

solution/0000-0099/0045.Jump Game II/README_EN.md

+40
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,46 @@ public class Solution {
138138
}
139139
```
140140

141+
### **C**
142+
143+
```c
144+
#define min(a, b) a < b ? a : b
145+
int jump(int* nums, int numsSize){
146+
int dp[numsSize];
147+
for (int i = 0; i < numsSize; i++) {
148+
dp[i] = numsSize;
149+
}
150+
dp[0] = 0;
151+
for (int i = 0; i < numsSize - 1; i++) {
152+
for (int j = i + 1; j < (min(i + nums[i] + 1, numsSize)); j++) {
153+
dp[j] = min(dp[j], dp[i] + 1);
154+
}
155+
}
156+
return dp[numsSize - 1];
157+
}
158+
```
159+
160+
### **Rust**
161+
162+
```rust
163+
impl Solution {
164+
pub fn jump(nums: Vec<i32>) -> i32 {
165+
let n = nums.len();
166+
let mut dp = vec![i32::MAX; n];
167+
dp[0] = 0;
168+
for i in 0..n - 1 {
169+
for j in 1..=nums[i] as usize {
170+
if i + j >= n {
171+
break;
172+
}
173+
dp[i + j] = dp[i + j].min(dp[i] + 1);
174+
}
175+
}
176+
dp[n - 1]
177+
}
178+
}
179+
```
180+
141181
### **...**
142182

143183
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#define min(a, b) a < b ? a : b
2+
int jump(int* nums, int numsSize){
3+
int dp[numsSize];
4+
for (int i = 0; i < numsSize; i++) {
5+
dp[i] = numsSize;
6+
}
7+
dp[0] = 0;
8+
for (int i = 0; i < numsSize - 1; i++) {
9+
for (int j = i + 1; j < (min(i + nums[i] + 1, numsSize)); j++) {
10+
dp[j] = min(dp[j], dp[i] + 1);
11+
}
12+
}
13+
return dp[numsSize - 1];
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn jump(nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let mut dp = vec![i32::MAX; n];
5+
dp[0] = 0;
6+
for i in 0..n - 1 {
7+
for j in 1..=nums[i] as usize {
8+
if i + j >= n {
9+
break;
10+
}
11+
dp[i + j] = dp[i + j].min(dp[i] + 1);
12+
}
13+
}
14+
dp[n - 1]
15+
}
16+
}

solution/0500-0599/0513.Find Bottom Left Tree Value/README.md

+92
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,98 @@ func findBottomLeftValue(root *TreeNode) int {
383383
}
384384
```
385385

386+
### **Rust**
387+
388+
```rust
389+
// Definition for a binary tree node.
390+
// #[derive(Debug, PartialEq, Eq)]
391+
// pub struct TreeNode {
392+
// pub val: i32,
393+
// pub left: Option<Rc<RefCell<TreeNode>>>,
394+
// pub right: Option<Rc<RefCell<TreeNode>>>,
395+
// }
396+
//
397+
// impl TreeNode {
398+
// #[inline]
399+
// pub fn new(val: i32) -> Self {
400+
// TreeNode {
401+
// val,
402+
// left: None,
403+
// right: None
404+
// }
405+
// }
406+
// }
407+
use std::rc::Rc;
408+
use std::cell::RefCell;
409+
use std::collections::VecDeque;
410+
impl Solution {
411+
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
412+
let mut queue = VecDeque::new();
413+
queue.push_back(root);
414+
let mut res = 0;
415+
while !queue.is_empty() {
416+
res = queue.front().unwrap().as_ref().unwrap().borrow_mut().val;
417+
for _ in 0..queue.len() {
418+
let node = queue.pop_front().unwrap();
419+
let mut node = node.as_ref().unwrap().borrow_mut();
420+
if node.left.is_some() {
421+
queue.push_back(node.left.take());
422+
}
423+
if node.right.is_some() {
424+
queue.push_back(node.right.take());
425+
}
426+
}
427+
}
428+
res
429+
}
430+
}
431+
```
432+
433+
```rust
434+
// Definition for a binary tree node.
435+
// #[derive(Debug, PartialEq, Eq)]
436+
// pub struct TreeNode {
437+
// pub val: i32,
438+
// pub left: Option<Rc<RefCell<TreeNode>>>,
439+
// pub right: Option<Rc<RefCell<TreeNode>>>,
440+
// }
441+
//
442+
// impl TreeNode {
443+
// #[inline]
444+
// pub fn new(val: i32) -> Self {
445+
// TreeNode {
446+
// val,
447+
// left: None,
448+
// right: None
449+
// }
450+
// }
451+
// }
452+
use std::rc::Rc;
453+
use std::cell::RefCell;
454+
use std::collections::VecDeque;
455+
impl Solution {
456+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, cur: i32, max: &mut i32, res: &mut i32) {
457+
if root.is_none() {
458+
return;
459+
}
460+
let root = root.as_ref().unwrap().borrow();
461+
Self::dfs(&root.left, cur + 1, max, res);
462+
Self::dfs(&root.right, cur + 1, max, res);
463+
if *max < cur {
464+
*max = cur;
465+
*res = root.val;
466+
}
467+
}
468+
469+
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
470+
let mut max = 0;
471+
let mut res = 0;
472+
Self::dfs(&root, 1, &mut max,&mut res);
473+
res
474+
}
475+
}
476+
```
477+
386478
### **...**
387479

388480
```

solution/0500-0599/0513.Find Bottom Left Tree Value/README_EN.md

+92
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,98 @@ func findBottomLeftValue(root *TreeNode) int {
359359
}
360360
```
361361

362+
### **Rust**
363+
364+
```rust
365+
// Definition for a binary tree node.
366+
// #[derive(Debug, PartialEq, Eq)]
367+
// pub struct TreeNode {
368+
// pub val: i32,
369+
// pub left: Option<Rc<RefCell<TreeNode>>>,
370+
// pub right: Option<Rc<RefCell<TreeNode>>>,
371+
// }
372+
//
373+
// impl TreeNode {
374+
// #[inline]
375+
// pub fn new(val: i32) -> Self {
376+
// TreeNode {
377+
// val,
378+
// left: None,
379+
// right: None
380+
// }
381+
// }
382+
// }
383+
use std::rc::Rc;
384+
use std::cell::RefCell;
385+
use std::collections::VecDeque;
386+
impl Solution {
387+
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
388+
let mut queue = VecDeque::new();
389+
queue.push_back(root);
390+
let mut res = 0;
391+
while !queue.is_empty() {
392+
res = queue.front().unwrap().as_ref().unwrap().borrow_mut().val;
393+
for _ in 0..queue.len() {
394+
let node = queue.pop_front().unwrap();
395+
let mut node = node.as_ref().unwrap().borrow_mut();
396+
if node.left.is_some() {
397+
queue.push_back(node.left.take());
398+
}
399+
if node.right.is_some() {
400+
queue.push_back(node.right.take());
401+
}
402+
}
403+
}
404+
res
405+
}
406+
}
407+
```
408+
409+
```rust
410+
// Definition for a binary tree node.
411+
// #[derive(Debug, PartialEq, Eq)]
412+
// pub struct TreeNode {
413+
// pub val: i32,
414+
// pub left: Option<Rc<RefCell<TreeNode>>>,
415+
// pub right: Option<Rc<RefCell<TreeNode>>>,
416+
// }
417+
//
418+
// impl TreeNode {
419+
// #[inline]
420+
// pub fn new(val: i32) -> Self {
421+
// TreeNode {
422+
// val,
423+
// left: None,
424+
// right: None
425+
// }
426+
// }
427+
// }
428+
use std::rc::Rc;
429+
use std::cell::RefCell;
430+
use std::collections::VecDeque;
431+
impl Solution {
432+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, cur: i32, max: &mut i32, res: &mut i32) {
433+
if root.is_none() {
434+
return;
435+
}
436+
let root = root.as_ref().unwrap().borrow();
437+
Self::dfs(&root.left, cur + 1, max, res);
438+
Self::dfs(&root.right, cur + 1, max, res);
439+
if *max < cur {
440+
*max = cur;
441+
*res = root.val;
442+
}
443+
}
444+
445+
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
446+
let mut max = 0;
447+
let mut res = 0;
448+
Self::dfs(&root, 1, &mut max,&mut res);
449+
res
450+
}
451+
}
452+
```
453+
362454
### **...**
363455

364456
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
use std::collections::VecDeque;
22+
impl Solution {
23+
pub fn find_bottom_left_value(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
24+
let mut queue = VecDeque::new();
25+
queue.push_back(root);
26+
let mut res = 0;
27+
while !queue.is_empty() {
28+
res = queue.front().unwrap().as_ref().unwrap().borrow_mut().val;
29+
for _ in 0..queue.len() {
30+
let node = queue.pop_front().unwrap();
31+
let mut node = node.as_ref().unwrap().borrow_mut();
32+
if node.left.is_some() {
33+
queue.push_back(node.left.take());
34+
}
35+
if node.right.is_some() {
36+
queue.push_back(node.right.take());
37+
}
38+
}
39+
}
40+
res
41+
}
42+
}

0 commit comments

Comments
 (0)