151. 反转字符串中的单词
This commit is contained in:
+3
-43
@@ -3,50 +3,10 @@ mod tree;
|
|||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn my_atoi(s: String) -> i32 {
|
pub fn reverse_words(s: String) -> String {
|
||||||
let mut ans = 0_i64;
|
s.split(' ').filter(|s| !s.is_empty()).rev().collect::<Vec<_>>().join(" ")
|
||||||
let mut neg = false;
|
|
||||||
let s = s.chars().collect::<Vec<_>>();
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{:?}", Solution::my_atoi("1337c0d3".to_string()));
|
println!("{:?}", Solution::reverse_words("a good example".to_string()));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user