[Swift]LeetCode58. 最後一個單詞的長度 | Length of Last Word

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xkuzpgoj-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.git

If the last word does not exist, return 0.github

Note: A word is defined as a character sequence consists of non-space characters only.微信

Example:ide

Input: "Hello World"
Output: 5

給定一個僅包含大小寫字母和空格 ' ' 的字符串,返回其最後一個單詞的長度。spa

若是不存在最後一個單詞,請返回 0 。rest

說明:一個單詞是指由字母組成,但不包含任何空格的字符串。code

示例:component

輸入: "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 }

12mshtm

 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 }
相關文章
相關標籤/搜索