[LintCode] 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".
- 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.
LeetCode上的原题,请参见我之前的博客Reverse Words in a String。
解法一:
class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
int storeIndex = , n = s.size();
reverse(s.begin(), s.end());
for (int i = ; i < n; ++i) {
if (s[i] != ' ') {
if (storeIndex != ) 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;
}
}
return string(s.begin(), s.begin() + storeIndex);
}
};
解法二:
class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
string res = "", t = "";
istringstream is(s);
while (getline(is, t, ' ')) {
if (t.empty()) continue;
res = (res.empty() ? t : (t + " " + res));
}
return res;
}
};
解法三:
class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
istringstream is(s);
is >> s;
string t;
while (is >> t) {
s = t + " " + s;
}
return (s[] == ' ') ? "" : s;
}
};
[LintCode] 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 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "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 ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
随机推荐
- FusionCharts-堆栈图、xml格式、刷新数据、添加事件link、传参
*起因* 本来想用Chart.js来搞图表的, 但是来了个新需求,想搞的华丽点,毕竟对Chart.js来说,实现有点难度, *做出的改变* 最终选择了FusionCharts, *难点* 网上关于Fu ...
- Authcode()
加密解密函数Authcode(): 1. // 参数解释 2. // $string: 明文 或 密文 3. // $operation:DECODE表示解密,其它表示加密 4. // ...
- PHP判断变量是否存在及函数isset() 、empty()与is_null的区别
一.举例说明 A.如何判断一个变量是否定义? <?php // 假设不存在$test 变量 if (isset($test)) { echo '$test 已经set', '<br/> ...
- 《征服 C 指针》摘录6:解读 C 的声明
一.混乱的声明——如何自然地理解 C 的声明? 通常,C 的声明 int hoge; 这样,使用“类型 变量名;”的形式进行书写. 可是,像“指向 int 的指针”类型的变量,却要像下面这样进行声明: ...
- 【MySQL】使用Length和Cast函数计算TEXT类型字段的长度
背景: 前段时间,业务需要,为了快速让解析的Excel入库,所以把不是很确定的字段全部设置成了TEXT. 今天需要进行表结构优化,把字段长度控制在合适的范围,并尽量不使用TEXT类型. -- 计算长度 ...
- at 常用命令
以debian 6.0.1 为例: 服务开启关闭: Usage: /etc/init.d/atd {start|stop|restart|force-reload|status} 设置一次计划任务(a ...
- 外网通过root账户访问虚拟机mysql
http://www.cnblogs.com/live41/archive/2013/04/02/2995178.html
- jstl 简单实践
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 01 java开发前小知识
1.常见的dos命令 盘符: 进入指定的盘符下. dir : 列出当前目录下的文件以及文件夹 md : 创建目录 rd : 删除目录 注意:rd不能删除非空的文件夹,而且只能用于删除文件夹. c ...
- 单节点下多个Tomcat服务器并存的端口号配置
一个服务器节点同时安装多个tomcat服务器时,如果仅仅修改访问端口号则会提示端口冲突启动失败,还需要修改另外端口号解决,一共需要修改3处地方,修改如下: 编辑配置文件:server.xml 1.首先 ...