C# simple swap solution


  • 0
    D
    public string ReverseString(string s) {
            var array = new char[s.Length];
            var halfLength = s.Length / 2;
            
            if (s.Length % 2 != 0)
            {
                halfLength++;
            }
            
            for(var i=0; i<halfLength; i++)
            {
                var temp = s[i];
                var endIndex = s.Length-i-1;
                
                array[i] = s[endIndex];
                array[endIndex] = temp;
            }
            
            return new string(array);
        }
    

Log in to reply
 

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