Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - 557. Reverse Words in a String III
- func reverseWords(_ s: String) -> String {
- var s = Array(s).map{String($0)}
- var l = 0
- for r in 0..<s.count {
- if s[r] == " " || r == s.count - 1 {
- var tempR = r - 1
- var tempL = l
- if r == s.count - 1 {
- tempR = r
- }
- while tempL < tempR {
- s.swapAt(tempL, tempR)
- tempL += 1
- tempR -= 1
- }
- l = r + 1
- }
- }
- return s.joined()
- }
- // если много пробелов то тоже работает
- reverseWords("Let's take LeetCode contest ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement