To prove safety Offer_ programming problem _ Flip word order of the columns

Title Description

Cattle recently came off a new employee Fish, every morning and always will be holding a magazine in English, write some sentences in the book. Cat Fish wrote to colleagues interested in the content, look to the day he borrowed Fish, but not read its meaning. For example, "student. A am I". Later I realized that this guy had the sentence word order reversed, the correct sentence should be "I am a student.". Cat on the order of every one of these words do not flip the line, can you help him?

AC Code

public class Solution {
    public String ReverseSentence(String str) {
       if (str.length() == 0 || str == null)
            return str;
        String space[] = str.split(" ");
        if (space.length == 0)
            return str;
        String temp = "";
        for (int i = space.length - 1; i >= 0; i--)
            if (i == 0)
                temp += space[i];
            else
                temp += space[i] + " ";
        return temp; 
    }
}

Topic analysis

( "") Into a string, the string into the array using string.split. Then from the back into a new connection traversal string.

Guess you like

Origin www.cnblogs.com/jiangyanblog/p/11666600.html