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 中使用至少一个空格将字符串中的 单词 分隔 ...
随机推荐
- ssh原理【转】
1 转自 http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 2 ssh远程登陆的原理 普通用户远程登陆 ssh jason@ho ...
- CH 5105 Cookies(贪心+DP)
\(CH 5105 Cookies\) \(solution:\) 真是好题一道!这道题我想了很久很久,就得这一题可以直接完全贪心,可惜最后还是失败了,但是对贪心的深入思考也换来了一个最优解方案.然后 ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- Fibonacci数列(找规律)
题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...
- django rest_framework swagger使用案例
环境准备 环境要求: python3 django2 pip3 模块安装: pip3 install django-rest-framework pip3 install django-rest-sw ...
- poj 2923 Relocation 解题报告
题目链接:http://poj.org/problem?id=2923 题目意思:给出两部卡车能装的最大容量,还有n件物品的分别的weight.问以最优方式装入,最少能运送的次数是多少. 二进制表示物 ...
- Oracle安装:silent安装方式
之前一直是通过图形界面来安装oracle,这次上机考试说用silent (静默)形式安装.一点头绪都没有,虽然当时提供了oracle官方文档. 遂查找资料,安装了一下: 一.准备工作: 1.系统参数调 ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- setTimeout的第三个参数
最熟悉的地方,往往会忽略一些细节.就比如 setTimeout 函数,做前端开发的同学都会很熟悉这个函数,经常使用这个函数,但是知道这个函数还有第三个参数的小伙伴可能就不多了.起码我在阅读阮老师的 e ...
- CKD 实现
主要功能: 1.新物料(部品号)的入库管理 部品号的验证.描述.品名.重量.单价等 2.部品号-供应商的核对 校验部品号/供应商的对应情况.入库.移除等 3.BOM清单的导入 基础清单的导入 4.订单 ...