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

前端 未结 21 1797
[愿得一人]
[愿得一人] 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:41

    You would use what is known as an iterative recursive function, which is O(N) in time as it takes N (N being the number of words) iterations to complete and O(1) in space as each iteration holds its own state within the function arguments.

    (define (reverse sentence-to-reverse)
      (reverse-iter (sentence-to-reverse ""))
    
    (define (reverse-iter(sentence, reverse-sentence)
      (if (= 0 string-length sentence)
        reverse-sentence
        ( reverse-iter( remove-first-word(sentence), add-first-word(sentence, reverse-sentence)))
    

    Note: I have written this in scheme which I am a complete novice, so apologies for lack of correct string manipulation.

    remove-first-word finds the first word boundary of sentence, then takes that section of characters (including space and punctuation) and removes it and returns new sentence

    add-first-word finds the first word boundary of sentence, then takes that section of characters (including space and punctuation) and adds it to reverse-sentence and returns new reverse-sentence contents.

    0 讨论(0)
  • 2020-11-28 04:44
    #include <string>
    #include <boost/next_prior.hpp>
    
    void reverse(std::string& foo) {
        using namespace std;
        std::reverse(foo.begin(), foo.end());
        string::iterator begin = foo.begin();
        while (1) {
            string::iterator space = find(begin, foo.end(), ' ');
            std::reverse(begin, space);
            begin = boost::next(space);
            if (space == foo.end())
                break;
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 04:47

    in Ruby

    "this is a string".split.reverse.join(" ")

    0 讨论(0)
  • 2020-11-28 04:47

    in C#, in-place, O(n), and tested:

    static char[] ReverseAllWords(char[] in_text)
    {
        int lindex = 0;
        int rindex = in_text.Length - 1;
        if (rindex > 1)
        {
            //reverse complete phrase
            in_text = ReverseString(in_text, 0, rindex);
    
            //reverse each word in resultant reversed phrase
            for (rindex = 0; rindex <= in_text.Length; rindex++)
            {
                if (rindex == in_text.Length || in_text[rindex] == ' ')
                {
                    in_text = ReverseString(in_text, lindex, rindex - 1);
                    lindex = rindex + 1;
                }
            }
        }
        return in_text;
    }
    
    static char[] ReverseString(char[] intext, int lindex, int rindex)
    {
        char tempc;
        while (lindex < rindex)
        {
            tempc = intext[lindex];
            intext[lindex++] = intext[rindex];
            intext[rindex--] = tempc;
        }
        return intext;
    }
    
    0 讨论(0)
  • 2020-11-28 04:47

    A one liner:

    l="Is this as expected ??"
    " ".join(each[::-1] for each in l[::-1].split())
    

    Output:

    '?? expected as this Is'
    
    0 讨论(0)
提交回复
热议问题