给定一个字符串,翻转字符串中的每个单词。
例如,
给定 s = "the sky is blue",
返回 "blue is sky the"。
对于C程序员:请尝试用O(1) 时间复杂度的原地解法。
说明:
    什么构成一个词?
    一系列非空格字符组成一个词。
    输入字符串是否可以包含前导或尾随空格?
    是。但是,您的反转字符串不应包含前导或尾随空格。
    两个单词之间多空格怎么样?
    将它们缩小到反转字符串中的单个空格。
详见:https://leetcode.com/problems/reverse-words-in-a-string/description/

实现语言:Java

方法一:

public class Solution {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
StringBuilder result = new StringBuilder();
for (int idx = words.length - 1; idx >= 0; idx--) {
result.append(words[idx]+" ");
}
return result.toString().trim();
}
}

方法二:

public class Solution {
public String reverseWords(String str) {
String[] words = str.trim().split(" +");
Collections.reverse(Arrays.asList(words));
return String.join(" ", words);
}
}

方法三:

public class Solution {
public String reverseWords(String str) {
String result = "";
String[] words = str.trim().split(" ");
Stack<String> stack = new Stack<>();
for (String s : words) {
if (s.length() > 0) {
stack.push(s);
}
}
while (!stack.isEmpty()) {
result += (stack.pop() + (stack.size() > 0 ? " " : ""));
}
return result;
}
}

参考:http://www.cnblogs.com/grandyang/p/4606676.html

151 Reverse Words in a String 翻转字符串里的单词的更多相关文章

  1. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...

  2. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  3. Leetcode151. Reverse Words in a String翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...

  4. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  5. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  6. 151. Reverse Words in a String翻转一句话中的单词

    [抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...

  7. LeetCode 151. 翻转字符串里的单词(Reverse Words in a String)

    151. 翻转字符串里的单词 151. Reverse Words in a String

  8. C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. 【LeetCode】151. 翻转字符串里的单词(剑指offer 58-I)

    151. 翻转字符串里的单词 知识点:字符串:双指针 题目描述 给你一个字符串 s ,逐个翻转字符串中的所有 单词 . 单词 是由非空格字符组成的字符串.s 中使用至少一个空格将字符串中的 单词 分隔 ...

随机推荐

  1. Mac OS用docker Desktop安装单节点kubernetes

    方案: 安装方式:阿里云minikube,k8s官方minikube,kubeadm, docker Desktop中自带第k8s 安装环境:在linux虚拟机中安装k8s,在macos中安装k8s, ...

  2. poj 1015 Jury Compromise(背包+方案输出)

    \(Jury Compromise\) \(solution:\) 这道题很有意思,它的状态设得很...奇怪.但是它的数据范围实在是太暴露了.虽然当时还是想了好久好久,出题人设了几个限制(首先要两个的 ...

  3. LCS的几种求法

    \(LCS:\) 对于两个长度均为 \(N\) 的数列 \(A\) 和 \(B\) ,存在一个数列 \(C\) 使得 \(C\) 既是 \(A\) 的子序列有事 \(B\) 的子序列,现在需要求这个数 ...

  4. 使用proc接口例子【转】

    本文转载自:http://blog.csdn.net/mike8825/article/details/52434666 版权声明:本文为博主原创文章,未经博主允许不得转载. 在上一篇的使用sys接口 ...

  5. hihocoder 1082 然而沼跃鱼早就看穿了一切 (替换指定的串 )

    #1082 : 然而沼跃鱼早就看穿了一切 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句 ...

  6. POJ3259 Wormholes —— spfa求负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  7. get all sites under IIS

    https://stackoverflow.com/questions/2555668/how-to-programmatically-get-sites-list-and-virtual-dirs- ...

  8. Getting Started with xUnit.net (desktop)

    https://xunit.github.io/docs/getting-started-desktop.html In this article, we will demonstrate getti ...

  9. YTU 2451: 股市风云

    2451: 股市风云 时间限制: 1 Sec  内存限制: 128 MB 提交: 37  解决: 25 [提交][状态][讨论版] 题目描述 股市强烈动荡,有涨有跌.现在有一组数据表示各公司的涨跌(涨 ...

  10. vue 更改头像功能实现

     ——————– 如上图所示:需要完成的功能是点击更改头像,获取本地文件库,选择图片后将原始图片替换.这里我就直接用html文件引入vue来简单实现在这功能,代码如下: HTML: <div i ...