From 349a99051261c38dd32f0eee29295e8242117194 Mon Sep 17 00:00:00 2001 From: li-chx Date: Fri, 14 Aug 2026 20:39:52 +0800 Subject: [PATCH] =?UTF-8?q?129.=20=E6=B1=82=E6=A0=B9=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=88=B0=E5=8F=B6=E8=8A=82=E7=82=B9=E6=95=B0=E5=AD=97=E4=B9=8B?= =?UTF-8?q?=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index d05e0d0..4b1040f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,26 +5,31 @@ mod list; mod tree; struct Solution {} + +use crate::tree::{TreeNode, build_tree_from_str}; +use std::cell::RefCell; +use std::rc::Rc; + impl Solution { - pub fn maximum_length_substring(s: String) -> i32 { - let s = s.bytes().map(|x| x as usize).collect::>(); - let mut arr = vec![0; 'z' as usize + 1]; - let mut l = 0; - let mut ans = 0; - for r in 0..s.len() { - arr[s[r]] += 1; - while arr[s[r]] > 2 { - arr[s[l]] -= 1; - l += 1; - } - ans = ans.max(r - l + 1); + pub fn sum_numbers(root: Option>>) -> i32 { + Self::dfs(root).iter().map(|x| x.parse::().unwrap()).sum() + } + pub fn dfs(root: Option>>) -> Vec { + if root.is_none() { + return vec![]; } - ans as i32 + let x = root.as_ref().unwrap(); + let mut arr = Self::dfs(Rc::clone(x).borrow().left.clone()); + arr.append(&mut Self::dfs(Rc::clone(x).borrow().right.clone())); + if arr.is_empty() { + return vec![x.borrow().val.to_string()]; + } + arr.iter().map(|y| x.borrow().val.to_string() + y).collect() } } fn main() { println!( "{:?}", - Solution::maximum_length_substring("bcbbbcba".to_string()) + Solution::sum_numbers(build_tree_from_str("[4,9,0,5,1]")) ); }