Advertisement
HXXXXJ

186. Reverse Words in a String II

Mar 24th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.52 KB | None | 0 0
  1. func reverseWords(_ str: inout [Character]) {
  2.         var i = 0
  3.         while i < str.count {
  4.             let last = i
  5.             while i < str.count && str[i] != " "{
  6.                 i += 1
  7.             }
  8.             swap(&str, last, i - 1)
  9.             i += 1
  10.         }
  11.         str.reverse()
  12.     }
  13.    
  14.     func swap(_ str: inout [Character], _ s : Int, _ e : Int){
  15.         var i = s
  16.         var j = e
  17.         while i < j {
  18.             str.swapAt( i , j)
  19.             i += 1
  20.             j -= 1
  21.         }
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement