LCR 140. 训练计划 II
This commit is contained in:
+1
-1
@@ -7,7 +7,7 @@ pub struct ListNode {
|
|||||||
|
|
||||||
impl ListNode {
|
impl ListNode {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn new(val: i32) -> Self {
|
pub fn new(val: i32) -> Self {
|
||||||
ListNode {
|
ListNode {
|
||||||
next: None,
|
next: None,
|
||||||
val
|
val
|
||||||
|
|||||||
+19
-3
@@ -1,12 +1,28 @@
|
|||||||
|
use crate::list::{ListNode, make_list};
|
||||||
|
|
||||||
mod arr;
|
mod arr;
|
||||||
|
mod list;
|
||||||
mod tree;
|
mod tree;
|
||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn reverse_words(s: String) -> String {
|
pub fn training_plan(head: Option<Box<ListNode>>, cnt: i32) -> Option<Box<ListNode>> {
|
||||||
s.split(' ').filter(|s| !s.is_empty()).rev().collect::<Vec<_>>().join(" ")
|
let mut fast = &head;
|
||||||
|
|
||||||
|
for i in 0..cnt {
|
||||||
|
fast = &fast.as_ref()?.next;
|
||||||
|
}
|
||||||
|
let mut slow = &head;
|
||||||
|
while fast.is_some() {
|
||||||
|
slow = &slow.as_ref()?.next;
|
||||||
|
fast = &fast.as_ref()?.next;
|
||||||
|
}
|
||||||
|
slow.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{:?}", Solution::reverse_words("a good example".to_string()));
|
println!(
|
||||||
|
"{:?}",
|
||||||
|
Solution::training_plan(make_list(vec![2, 4, 7, 8]), 2)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user