[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",
return "blue is sky the".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
Clarification:
- What constitutes a word?
- A sequence of non-space characters constitutes a word.
- Could the input string contain leading or trailing spaces?
- Yes. However, your reversed string should not contain leading or trailing spaces.
- How about multiple spaces between two words?
- Reduce them to a single space in the reversed string.
把一个字符串中的单词逆序,单词字符顺序不变。
解法1: New array, 新建一个数组,把字符串以空格拆分成单词存到数组,在把单词逆序拷贝进新数组。
解法2: One place,不能新建数组,在原数组的基础上换位。把字符串的所以字符逆序,然后在把每个单词的字符逆序。
Java: New array, two pass
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
if(words.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder(words[words.length-1]);
for(int i=words.length-2; i >=0; i--) {
sb.append(" "+words[i]);
}
return sb.toString();
}
Java: New array, one pass
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
int end = s.length();
int i = end-1;
while(i>=0) {
if(s.charAt(i) == ' ') {
if(i < end-1) {
sb.append(s.substring(i+1, end)).append(" ");
}
end = i;
}
i--;
}
sb.append(s.substring(i+1, end));
return sb.toString().trim();
}
Java: New array, one pass
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
int last = s.length();
for(int i=s.length()-1; i>=-1; i--) {
if(i==-1 || s.charAt(i)==' ') {
String word = s.substring(i+1, last);
if(!word.isEmpty()) {
if(sb.length() != 0) sb.append(' ');
sb.append(word);
}
last = i;
}
}
return sb.toString();
}
Java:One place
public String reverseWords(String s) {
if(s == null || s.isEmpty()) return s;
char[] data = s.toChartArray();
int n = data.length;
reverse(data, 0, n-1);
int last = -1;
for(int i=0; i<=n; i++) {
if(i == n || data[i] == ' ') {
if(i-last>1) reverse(data, last+1, i-1);
last = i;
}
}
return new String(data);
}
private void reverse(char[] data, int start, int end) {
while(start < end) {
char tmp = data[start];
data[start++] = data[end];
data[end--] = tmp;
}
}
Python: New array
class Solution:
# @param s, a string
# @return a string
def reverseWords(self, s):
return ' '.join(reversed(s.split()))
C++:
class Solution {
public:
void reverseWords(string &s) {
int storeIndex = 0, n = s.size();
reverse(s.begin(), s.end());
for (int i = 0; i < n; ++i) {
if (s[i] != ' ') {
if (storeIndex != 0) s[storeIndex++] = ' ';
int j = i;
while (j < n && s[j] != ' ') s[storeIndex++] = s[j++];
reverse(s.begin() + storeIndex - (j - i), s.begin() + storeIndex);
i = j;
}
}
s.resize(storeIndex);
}
};
类似题目:
[LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
[LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
All LeetCode Questions List 题目汇总
[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词的更多相关文章
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
随机推荐
- Supermarket(贪心/并查集)
题目链接 原创的博客 题意: 超市里有N个商品. 第i个商品必须在保质期(第di天)之前卖掉, 若卖掉可让超市获得pi的利润. 每天只能卖一个商品. 现在你要让超市获得最大的利润. n , p[i], ...
- MacOS安装Docker
傻瓜式安装: 1. 浏览器或命令行下载:https://download.docker.com/mac/stable/Docker.dmg 2. 点击安装文件,拖动图标到应用 3. 确认安装正常:do ...
- pipy配置镜像源
新电脑第一次使用使用pip命令下载贼慢 我们需要使用国内pipy镜像,参考如下 https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ 所以只要设置一下就行了: ...
- Springboot整合通用mapper
通用Mapper的分享使用 参考博客 Mybatis的通用mapper和Hibernate一样都实现了JPA接口,简化了数据库的操作 和Hibernate的对比 Hibernate和Mybatis都是 ...
- BZOJ 4890: [Tjoi2017]城市 树形dp
标签:树形dp,枚举,树的直径 一上来看到这个题就慌了,只想到了 $O(n^3)$ 的做法. 碰到这种题时要一步一步冷静地去分析,观察数据范围. 首先,$n\leqslant 5000$,所以可以先 ...
- 通过USB 2.0电缆手动设置内核模式调试
Windows的调试工具支持通过USB 2.0电缆进行内核调试.本文介绍如何手动设置USB 2.0调试.通过USB 2.0电缆进行调试需要以下硬件: USB 2.0调试电缆.此电缆不是标准USB 2. ...
- circus docker image web 运行异常问题的解决
经过查看官方文档,因为我使用的是python 较高版本,存在兼容问题,解决方法 修改基础镜像版本 代码如下: FROM python:2.7-slim-stretch LABEL AUTHOR=&qu ...
- 函数(定义、参数、return、变量、作用域、预解析)
一.函数定义 1.方式一 function 函数名(参数){ 函数体 }——————函数声明的方法 function fn(a){ console.log(a); }: 2.方式二 ...
- PHP.INI生成环境配置文件
extension_dir = /home/php/lib/php/extensions/no-debug-zts- zend_extension = opcache.so extension = p ...
- Codevs 3122 奶牛代理商 VIII(状压DP)
3122 奶牛代理商 VIII 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 小徐是USACO中国区的奶牛代理商,专门出售质优 ...