151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词。
例如,
给定 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 翻转字符串里的单词的更多相关文章
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- [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& ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- LeetCode 151. 翻转字符串里的单词(Reverse Words in a String)
151. 翻转字符串里的单词 151. Reverse Words in a String
- C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【LeetCode】151. 翻转字符串里的单词(剑指offer 58-I)
151. 翻转字符串里的单词 知识点:字符串:双指针 题目描述 给你一个字符串 s ,逐个翻转字符串中的所有 单词 . 单词 是由非空格字符组成的字符串.s 中使用至少一个空格将字符串中的 单词 分隔 ...
随机推荐
- Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP
D. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
- string和int互相转化
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- REST RPC HTTP vs 高性能二进制协议 序列化和通信协议
edisonchou https://mp.weixin.qq.com/s/-XZXqXawR-NxJMPCeiNsmg .NET Core微服务之服务间的调用方式(REST and RPC) Edi ...
- html5--6-7 CSS选择器4
html5--6-7 CSS选择器4 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓选择器就是样式作 ...
- html5--6-51 阶段练习3-旋转导航
html5--6-51 阶段练习3-旋转导航 @charset="UTF-8"; ul{ list-style: none; font-size:24px; font-weight ...
- driver, module以及怎么看他们
1. driver和module的区别 https://unix.stackexchange.com/questions/47208/what-is-the-difference-between-ke ...
- CodeForces-607B:Zuma (基础区间DP)
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the ...
- bzoj2337 XOR和路径——高斯消元
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2337 异或就一位一位考虑: x为到n的概率,解方程组即可: 考虑了n就各种蜜汁错误,所以索性 ...
- RDA TDT & TOT
首先看下面的TS PSI分析图: 注意:TOT UTC与TDT是一致的 TDT下的时间为: UTC+手动TIMEZONE TOT下的时间为: UTC+解析的time_offset time_offes ...
- 利用thrift在c++、java和python之间相互调用
转自:http://blog.csdn.net/andy_yf/article/details/7487384 thrift做为跨语言调用的方案有高效,支持语言较多,成熟等优点:代码侵入较强是其弱点. ...