题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
import Foundation
while let line = readLine() {
var string = ""
for ch in line {
if ch.isLetter {
string.append(ch)
} else {
if string.count > 0 && string.last != " " {
string.append(" ")
}
}
}
//! 可以取巧使用系统函数 不然就需要自己实现。或者参考 HJ13 句子逆序
let array = string.components(separatedBy: " ")
let resString = array.reversed().joined(separator: " ")
// let parts = line.split(separator: " ")
print(resString)
}

查看4道真题和解析