Show me the code
swift
class Solution {
func lengthOfLastWord(_ s: String) -> Int {
s.trimmingCharacters(in: .whitespacesAndNewlines).components(separatedBy: CharacterSet.whitespaces).last?.count ?? 0
}
}
注意点
swift
// print: ["", "", "", "fly", "me", "", "", "to", "", "", "the", "moon", "", ""]
print(" fly me to the moon ".components(separatedBy: CharacterSet.whitespaces))