Efficiently reverse the order of the words (not characters) in an array of characters

前端 未结 21 1887
[愿得一人]
[愿得一人] 2020-11-28 04:40

Given an array of characters which forms a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.

Example input and

21条回答
  •  情深已故
    2020-11-28 04:45

    using System;
    
    namespace q47407
    {
        class MainClass
        {
            public static void Main(string[] args)
            {
                string s = Console.ReadLine();
                string[] r = s.Split(' ');
                for(int i = r.Length-1 ; i >= 0; i--)
                    Console.Write(r[i] + " ");
                Console.WriteLine();
    
            }
        }
    }
    

    edit: i guess i should read the whole question... carry on.

提交回复
热议问题