[Swift]LeetCode557. 反转字符串中的单词 III | Reverse Words in a String III

时间:2021-10-20 15:49:07

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" 

注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。


空格:CharacterSet.whitespaces

376ms 

 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 }