58. 最后一个单词的长度
Show me the code
class Solution {func lengthOfLastWord(_ s: String) -> Int {s.trimmingCharacters(in: .whitespacesAndNewlines).components(separatedBy: CharacterSet.whitespaces).last?.count ?? 0}
}
注意点
// print: ["", "", "", "fly", "me", "", "", "to", "", "", "the", "moon", "", ""]
print(" fly me to the moon ".components(separatedBy: CharacterSet.whitespaces))