Used Array.Reverse instead of write own code, and runtime 55ms


  • -1
    S
    public class Solution {
    public void ReverseWords(char[] s) {
             var count = s.Length;
    
            if (count < 2) return;
    
            Array.Reverse(s);
    
            var start = 0;
    
            for (var i = 0; i < count; i++)
            {
                if (s[i] == ' ')
                {
                    Array.Reverse(s, start, i - start);
                    start = i + 1;
                }
    
                //take care the last word
                if (i == count - 1)
                {
                    Array.Reverse(s, start, count - start);
                }
            }       
    }
    

    }


Log in to reply
 

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