Swift - Why is it not reversing "a ba"


  • 0
    U

    class Solution {
    func reverseString(_ s: String) -> String {
    if s.characters.count > 0{
    var buf = UInt8
    var y = buf.count - 1
    var length = buf.count - 1
    if length > 1
    {
    length = length / 2
    }
    print(buf)

        for x in 0..<length
        {
            
            buf[x] = buf[x] ^ buf[y]
            
            buf[y] = buf[y] ^ buf[x]
            
            buf[x] = buf[x] ^ buf[y]
            
            y = y - 1
        }
        
        return String(bytes: buf, encoding: .utf8)!
        }
        return s
    
    }
    

    }


  • 0
    U

    Got it working. |

    if length > 1
    {
    if length % 2 == 0{

    length = length / 2

    }else
    {
    length = (length / 2) + 1
    }
    }


Log in to reply
 

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