[Swift]LeetCode58. 最后一个单词的长度 | Length of Last Word
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697952.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
Input: "Hello World" Output: 5
给定一个仅包含大小写字母和空格 ' '
的字符串,返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 0 。
说明:一个单词是指由字母组成,但不包含任何空格的字符串。
示例:
输入: "Hello World" 输出: 5
8ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 var count = 0 4 for c in s.reversed() { 5 if count == 0 && c == " " { continue } 6 if c == " " { 7 return count 8 } 9 count += 1 10 } 11 return count 12 } 13 }
12ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 4 var len = 0 5 for str in s.reversed() { 6 if str != " "{ 7 len += 1 8 } 9 if str == " " && len != 0 { 10 break 11 } 12 } 13 return len 14 } 15 }
12ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 let s = Array(s) 4 var tail = s.count - 1 5 var result = 0 6 7 while tail >= 0 && s[tail] == " " { 8 tail -= 1 9 } 10 11 while tail >= 0 && s[tail] != " " { 12 tail -= 1 13 result += 1 14 } 15 16 return result 17 } 18 }
16ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 if (s.isEmpty == false) { 4 let fullNameArr = s.characters.split{$0 == " "}.map(String.init) 5 if (fullNameArr.isEmpty) { 6 return 0 7 } 8 return fullNameArr[fullNameArr.count - 1].count 9 } 10 return 0 11 } 12 }
16ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 return s.split(separator: " ").last?.count ?? 0 4 } 5 }
20ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 var result: Int = 0 4 let array: Array = s.components(separatedBy: "\(" ")") 5 for index in stride(from:array.count-1, to:-1, by:-1) { 6 if array[index].count > 0 { 7 result = array[index].count 8 break 9 } 10 } 11 return result 12 } 13 }
20ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 var sprlit = s.components(separatedBy: .whitespacesAndNewlines) 4 print(sprlit) 5 guard s.count > 1 else{ 6 let inputWord = sprlit[0].trimmingCharacters(in: .whitespaces) 7 return inputWord.count 8 } 9 var startCount = sprlit.count-1 10 var lastWordCount = 0 11 while ( startCount >= 0){ 12 print("StartCount",startCount) 13 var lastWord = sprlit[startCount] 14 15 lastWord = lastWord.trimmingCharacters(in: .whitespacesAndNewlines) 16 print("word:",lastWord) 17 18 19 if(lastWord != ""){ 20 lastWordCount = lastWord.count 21 return lastWordCount 22 } 23 startCount = startCount - 1 24 lastWordCount = lastWord.count 25 } 26 return lastWordCount 27 } 28 }
36ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 var lastWord = "" 4 var restartLastWord = false 5 for (index, c) in s.enumerated() { 6 if c == " " { 7 restartLastWord = true 8 } else { 9 if restartLastWord { 10 restartLastWord = false 11 lastWord = "" 12 } 13 lastWord += String(c) 14 } 15 } 16 17 return lastWord.count 18 } 19 }
36ms
1 class Solution { 2 func lengthOfLastWord(_ s: String) -> Int { 3 //将参数常量变为变量 4 var word = s 5 //最后一个单词的长度 6 var len:Int = 0 7 //字符串为空不存在最后一个单词返回 0 8 if word.isEmpty{return 0} 9 let wordCount:Int=word.count-1 10 //倒序遍历 11 for i in (0...wordCount).reversed() 12 { 13 //遍历到第一个空格退出循环 14 if word[word.index(word.startIndex, offsetBy: i)] == " " 15 { 16 continue 17 } 18 //如果不是空格则继续遍历,i>=len 放前面, 否则 "q" 这样的字符串会越界 19 while(i>=len && word[word.index(word.startIndex, offsetBy: i-len)] != " ") 20 { 21 len+=1 22 } 23 return len 24 } 25 return len 26 } 27 }