[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", ...
随机推荐
- DT图库列表修改内容标题字数
dt限制标题字数的方法有几种,最简单的是一种是: {dsubstr($t[title], 42, '..')} 还有一种是列表输出,也是网上常用的方法 <!--{tag("module ...
- 前端Map封装源码
源于后台思路,简单封装了一下Map插件,方便以后使用. function Map() { this.elements = new Array(); //获取MAP元素个数 this.size = fu ...
- HDU - 3535:AreYouBusy (分组背包)
题意:给你n个工作集合,给你T的时间去做它们.给你m和s,说明这个工作集合有m件事可以做,它们是s类的工作集合(s=0,1,2,s=0说明这m件事中最少得做一件,s=1说明这m件事中最多只能做一件,s ...
- web scraper——爬取知乎|微博用户数据模板【三】
前言 在这里呢,我就只给模板,不写具体的教程啦,具体的可以参考我之前写的博文. https://www.cnblogs.com/wangyang0210/p/10338574.html 模板 进入微博 ...
- Best practices for a new Go developer
https://blog.rubylearning.com/best-practices-for-a-new-go-developer-8660384302fc This year I had the ...
- Java代码写PDF-保全批单
前言:最近自己要开发一个保全批单模块,由于自己在平时没有怎么接触过批单类型模块,甲方给了自己一套word模板,自己看了一下,个险的模板比较简单,但是团险一看,自己比较蒙圈,询问需求负责人说word中的 ...
- cifar-10数据集的可视化
import numpy as np from PIL import Image import pickle import os CHANNEL = 3 WIDTH = 32 HEIGHT = 32 ...
- 使用Simian进行重复代码检测
一.概述 Simian是一个可跨平台使用的重复代码检测工具,有商用和免费两种使用渠道,官方网址为:http://www.harukizaemon.com/simian/installation.htm ...
- docker 下载安装镜像
docker安装成功后. 1.搜索镜像 # docker search java 可使用 docker search命令搜索存放在 Docker Hub(这是docker官方提供的存放所有docker ...
- learning scala repreated parameters