美文网首页Leetcode每日两题程序员
Leetcode 151. Reverse Words in a

Leetcode 151. Reverse Words in a

作者: ShutLove | 来源:发表于2017-10-29 20:45 被阅读101次

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();
}

相关文章

网友评论

    本文标题:Leetcode 151. Reverse Words in a

    本文链接:https://www.haomeiwen.com/subject/naqupxtx.html