给定一个字符串,翻转字符串中的每个单词。
例如,
给定 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. WebGIS中地图恢复初始位置及状态

    我想实现这么一个效果:地图任意缩放后,点击一个按钮,将立刻回到地图初始加载时的位置,并且是没有缩放的状态. 怎么办呢?最好的办法就是用Home按钮. <!DOCTYPE HTML> < ...

  2. hibernate面试点

    1.谈谈你对hibernate的认识和理解 01.全自动的ORM框架 02.子项目 03.面向对象的思想来解决操作数据库 01.hibernate是一个开放源代码的对象关系映射(ORM)框架,它对JD ...

  3. springboot对传参的拦截统一处理

    在学习某网<java秒杀系统方案优化>的课程中,学到了一种springboot对传参的拦截统一处理的方式,特记录一下. 如后台方法一般需要根据token从Session中获取User对象, ...

  4. ios状态栏的一些操作

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //显示 [UIApplication sharedA ...

  5. 静态代理、动态代理和cglib代理

    转:https://www.cnblogs.com/cenyu/p/6289209.html 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处 ...

  6. Vue中 key keep-alive

    keep-alive key <!DOCTYPE html> <html> <head> <title></title> <scrip ...

  7. js判断字符串是否包含某个字符串

    String对象的方法 1,indexOf() (推荐) 方法可返回某个指定的字符串值在字符串中首次出现的位置.如果要检索的字符串值没有出现,则该方法返回 -1 var str = "123 ...

  8. 以太坊EVM在安全性方面的考虑

    以太坊上用户编写的合约是不可控的,要保证这些合约能够正确执行并且不会影响区块链的稳定,虚拟机需要做安全方面的考虑. 1 在程序执行过程中采取的每个计算步骤都必须提前支付费用, 从而防止DoS攻击.先消 ...

  9. Oracle:imp导入imp-00000问题

    现场环境:window2008 . oracle11.2g  .客户端安装的是oracle10g一个简洁版 34M的. 在imp导入时,提示 Message 100 not found; No mes ...

  10. [ZJOI 2008] 骑士

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1040 [算法] 首先 , 题目中互相讨厌的关系构成了一棵基环森林 用拓扑排序找出环 ...