题解 | #单词倒排#
单词倒排
http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
思路: 遍历识别单个字符后存入 ss ,遇上非英文字母字符时就 push 入 v,反转后输出。
use std::io::{self, *};
fn main() {
let stdin = io::stdin();
for line in stdin.lock().lines() {
let ll = line.unwrap();
let s = Vec::from(ll);
let mut v: Vec<String> = vec![];
let mut ss = String::new();
let mut i = 0;
while i < s.len() {
if s[i] >= 65 && s[i] <= 90 || s[i] >= 97 && s[i] <= 122 {
ss.push(char::from(s[i]));
} else if !ss.is_empty() {
v.push(ss);
ss = String::new();
}
i += 1;
}
v.push(ss);
v.reverse();
for i in v {
print!("{} ",i);
}
}
}
用 Rust 刷华为机试HJ 文章被收录于专栏
用 Rust 刷 HJ100 题,只需要懂基础 Rust 语法就能看懂