LCR 140. 训练计划 II
This commit is contained in:
+1
-1
@@ -7,7 +7,7 @@ pub struct ListNode {
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
fn new(val: i32) -> Self {
|
||||
pub fn new(val: i32) -> Self {
|
||||
ListNode {
|
||||
next: None,
|
||||
val
|
||||
|
||||
+19
-3
@@ -1,12 +1,28 @@
|
||||
use crate::list::{ListNode, make_list};
|
||||
|
||||
mod arr;
|
||||
mod list;
|
||||
mod tree;
|
||||
|
||||
struct Solution {}
|
||||
impl Solution {
|
||||
pub fn reverse_words(s: String) -> String {
|
||||
s.split(' ').filter(|s| !s.is_empty()).rev().collect::<Vec<_>>().join(" ")
|
||||
pub fn training_plan(head: Option<Box<ListNode>>, cnt: i32) -> Option<Box<ListNode>> {
|
||||
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() {
|
||||
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