From a813a0b1d651f10448436167dff59b772d573227 Mon Sep 17 00:00:00 2001 From: li-chx Date: Fri, 14 Aug 2026 19:31:41 +0800 Subject: [PATCH] =?UTF-8?q?151.=20=E5=8F=8D=E8=BD=AC=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E4=B8=AD=E7=9A=84=E5=8D=95=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 46 +++------------------------------------------- 1 file changed, 3 insertions(+), 43 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5b4b30d..66c5b2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,50 +3,10 @@ mod tree; struct Solution {} impl Solution { - pub fn my_atoi(s: String) -> i32 { - let mut ans = 0_i64; - let mut neg = false; - let s = s.chars().collect::>(); - let mut start = 0; - for i in 0..s.len() { - if s[i] == ' ' { - start = i + 1; - continue; - } - start = i + if s[i] == '+' || s[i] == '-' { - if s[i] == '-' { - neg = true - } - 1 - } else { - 0 - }; - break; - } - for i in start..s.len() { - if s[i] == '0' { - start = i + 1; - continue; - } - break; - } - for i in start..s.len() { - let c = s[i]; - if c.is_ascii_digit() { - ans = ans * 10 + if neg { -1 } else { 1 } * (c as u8 - '0' as u8) as i64; - if ans > i32::MAX as i64 { - return i32::MAX; - } - if ans < i32::MIN as i64 { - return i32::MIN; - } - continue; - } - break; - } - ans as i32 + pub fn reverse_words(s: String) -> String { + s.split(' ').filter(|s| !s.is_empty()).rev().collect::>().join(" ") } } fn main() { - println!("{:?}", Solution::my_atoi("1337c0d3".to_string())); + println!("{:?}", Solution::reverse_words("a good example".to_string())); }