Swift Reverse String


  • 0
    F

    I got a compile error when I run the code: "Line 10: expression resolves to an unused l-value"
    But when I run the same code by using Xcode playground, it gave me the right answer.
    I added two lines in playground:

    let solution1 = Solution()
    solution1.reverseString("hello")
    

    Can anyone help me with this?

    class Solution {
        func reverseString(s: String) -> String {
            var stringArray = [Character]()
            var newS = ""
            if (s == "") {return s}
            for c: Character in s.characters {
                stringArray += [c]
            }
            var l = stringArray.count - 1
            for (l; l >= 0; l--) {
            /*
            for i in 0..<l/2 {
              var j: Character
              j = stringArray[l-i]
              stringArray[l-i] = stringArray[i]
              stringArray[i] = j
            }
            */
            let e = stringArray.removeAtIndex(l)
            newS.append(e)
            }
            return newS
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.