1. 程式人生 > >[Swift]LeetCode557. 反轉字符串中的單詞 III | Reverse Words in a String III

[Swift]LeetCode557. 反轉字符串中的單詞 III | Reverse Words in a String III

order bsp using 反轉 san 一個 temp etc nts

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let‘s take LeetCode contest"
Output: "s‘teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.


給定一個字符串,你需要反轉字符串中每個單詞的字符順序,同時仍保留空格和單詞的初始順序。

示例 1:

輸入: "Let‘s take LeetCode contest"
輸出: "s‘teL ekat edoCteeL tsetnoc" 

註意:在字符串中,每個單詞由單個空格分隔,並且字符串中不會有任何額外的空格。


376ms: 空格:CharacterSet.whitespaces

 1 class Solution {
 2     func reverseWords(_ s: String) -> String {
 3         //按照空格分割,轉換為數組
4 var arr:[String] = s.components(separatedBy: " ") 5 for i in 0..<arr.count 6 { 7 //反轉單詞 8 arr[i] = String(arr[i].reversed()) 9 } 10 //數組轉換為字符串 11 return arr.joined(separator: " ") 12 } 13 }

84ms

 1 class
Solution { 2 func reverseWords(_ s: String) -> String { 3 func reverseString(_ s: String) -> String { 4 5 var startIndex = 0 6 var endIndex = s.count-1 7 var array = s.cString(using:.utf8)! 8 while startIndex<endIndex { 9 let char = array[startIndex] 10 array[startIndex] = array[endIndex] 11 array[endIndex] = char 12 startIndex+=1 13 endIndex-=1 14 } 15 return String.init(utf8String: array)! 16 } 17 18 let array = s.components(separatedBy: " ") 19 var resultArray = [String]() 20 for i in 0..<array.count { 21 resultArray.append(reverseString(array[i])) 22 } 23 let result = resultArray.joined(separator: " ") 24 return result 25 } 26 }

92ms

 1 class Solution {
 2     func reverseWords(_ s: String) -> String {
 3         var result : String = ""
 4         var tempStr : String = ""
 5         var index = s.endIndex
 6 
 7         while index != s.startIndex {
 8             index = s.index(before: index)
 9             if s[index] == " " {
10                 result = tempStr+" "+result
11                 tempStr = ""
12             }else{
13                 tempStr.append(s[index])
14             }
15         }
16         if result.count>0 {
17             result.removeLast()
18             result = tempStr+" "+result
19         }else{
20             result = tempStr
21         }
22         return result
23     }
24 }

120ms

 1 class Solution {
 2     func reverseWords(_ s: String) -> String {
 3         
 4         var result = ""
 5         let words = s.components(separatedBy: " ")
 6         
 7         for word in words {
 8             let reversedWord = String(word.characters.reversed()) + " "
 9             result.append(reversedWord)
10             
11         }
12         return result.trimmingCharacters(in: .whitespacesAndNewlines)
13     }
14 }

128ms

 1 class Solution {
 2     func reverseWords(_ s: String) -> String {
 3         var strs = ""
 4         for str in s.components(separatedBy: " ") {
 5             strs.append(String(str.reversed()))
 6             strs.append(" ")
 7         }
 8         strs.removeLast()
 9         return strs   
10     }
11 }

128ms

 1 class Solution {
 2     func reverseWords(_ s: String) -> String {
 3         let arr = s.split(separator: " ").map {
 4             return $0.reversed()
 5         }
 6         
 7         var result = arr.reduce("", { $0 + " " + $1 })
 8         if !result.isEmpty {
 9             result.remove(at: result.startIndex)
10         }
11         return result
12     }
13 }

328ms

1 class Solution {
2     func reverseWords(_ s: String) -> String {
3         return String(s.split(separator: " ").reduce("") { 
4             $0 + $1.reversed() + " " 
5         }.dropLast());
6     }
7 }

720ms

1 class Solution {
2     func reverseWords(_ s: String) -> String {
3         let result = s.lazy.split(separator:" ").map{ $0.reversed()}.joined(separator:" ")
4         return String(result)
5     }
6 }

812ms

 1 class Solution {
 2     func reverseWords(_ s: String) -> String {
 3         let array = (s.split(separator: " "))
 4         var temp: [String] = []
 5         for str in array {
 6             
 7             let chars: [Character] = [Character](str)
 8             temp.append(chars.reversed().map{String.init($0)}.joined())
 9         }
10         return temp.joined(separator: " ")
11     }
12 }

[Swift]LeetCode557. 反轉字符串中的單詞 III | Reverse Words in a String III