Leetcode每日两题程序员

Leetcode 151. Reverse Words in a

2017-10-29  本文已影响101人  ShutLove

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

public String reverseWords(String s) {
    if (s == null || s.length() <= 1) {
        return s;
    }

    String res = "";
    String[] words = s.trim().split(" +");
    for (int i = words.length - 1; i >= 0; i--) {
        res += words[i] + " ";
    }

    return res.trim();
}
上一篇下一篇

猜你喜欢

热点阅读