题目

Given an input string, reverse the string word by word.

For example,

Given s = "the sky is blue",

return "blue is sky the".

click to show clarification.

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) 去除首尾空格和去除字符串中多余的空格,计划用两个函数实现;
(2) 翻转整个字符串
(3) 依次将每个单词翻转

代码

#include <iostream>
#include <string>
#include <cwctype>
#include <vector> using namespace std; // 去除字符串首尾的空格
string& trim(string &s)
{
if(s.empty())
return s; string::iterator iter;
for(iter = s.begin(); iter != s.end()&& iswspace(*iter++););
s.erase(s.begin(),--iter); for(iter = s.end(); iter != s.begin()&& iswspace(*--iter););
s.erase(++iter,s.end()); return s;
} // 翻转字符串中的某一段子串
void reverseString(string &s, int begin, int end)
{
for(; begin < end; begin++,end--)
{
char temp = s[begin];
s[begin] = s[end];
s[end] = temp;
}
} // 删除单词之间多余的空格
void delete_mult_spaces(string &s)
{
string::iterator blank;
string::iterator iter = s.begin();
while(iter != s.end())
{
if(iswspace(*iter))
{
blank = iter + 1;
while(iswspace(*blank))
s.erase(blank);
iter = blank;
}
iter++;
}
} void reverseWords(string &s)
{
if (s.empty())
return; s = trim(s);
delete_mult_spaces(s); reverseString(s, 0, s.length()-1); int wbegin,wend;
wbegin = wend = 0;
for(size_t i = 0; i < s.length(); i++)
{
if(s[i] == ' ')
{
reverseString(s,wbegin,wend-1);
wbegin = wend + 1;
}
wend++; // deal with the last word
if(i == s.length()-1)
reverseString(s,wbegin,wend-1);
}
} int main()
{
vector<string> strs;
strs.push_back("the sky is blue");
strs.push_back(" the sky is blue");
strs.push_back("the sky is blue "); strs.push_back("the sky is blue");
strs.push_back(" the sky is blue ");
strs.push_back(""); vector<string>::iterator iter;
for(iter = strs.begin(); iter != strs.end(); iter++)
{
reverseWords(*iter);
cout << *iter << endl;
}
return 0;
}

参考


LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)的更多相关文章

  1. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  2. [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& ...

  3. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...

  4. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  5. [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 ...

  6. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  7. Leetcode151. Reverse Words in a String翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...

  8. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  9. 345. Reverse Vowels of a String翻转字符串中的元音字母

    [抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  10. 151. Reverse Words in a String翻转一句话中的单词

    [抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...

随机推荐

  1. wordpress 访问其他数据库

    有时候我们需要在 Worpdress 中访问其他服务器上的数据库时,可以使用以下方式,在 functions.php 中添加如下代码: // 输入数据库配置参数$mydb = new wpdb('us ...

  2. windows 安装 Composer 报错

    错误信息如下: 解决方法: 在 extension = php_snmp.dll 前加上 ";" 然后重启 php 即可安装

  3. Redis (error) NOAUTH Authentication required.

    首先查看redis设置密码没 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "" ...

  4. ospf的路由更新和撤销总结

    首先ospf 的报文有:hello报文,主要作用ospf 邻居建立及维护.dd报文,主要作用主从选举,序列号主从的确认,mtu的协商(可选).lsr 报文,主要作用向邻居请求lsa.lsu报文,主要作 ...

  5. Xcode 导出C++项目在其他电脑执行

    1.  先找到C++项目的可执行文件的位置 https://blog.csdn.net/qq_34759481/article/details/82700587 2.  关于存储和加载文件的目录,使用 ...

  6. php7.4 更新特性

    PHP 7.4.0 Released! The PHP development team announces the immediate availability of PHP 7.4.0. This ...

  7. python中判断对象类型的函数——isinstance

    isinstance是Python中的一个内建函数.是用来判断一个对象的变量类型. isinstance(object, class-or-type-or-tuple) 如果参数object是clas ...

  8. mac下php配置

    打开/关闭服务 sudo apachectl start sudo apachectl stop 查看apche版本 apacectl -v 修改项目默认路径: 打开配置文件目录/private/et ...

  9. Java中的常量池(字符串常量池、class常量池和运行时常量池)

    转载. https://blog.csdn.net/zm13007310400/article/details/77534349 简介: 这几天在看Java虚拟机方面的知识时,看到了有几种不同常量池的 ...

  10. 选择类排序 (简单选择排序,堆排序)— c语言实现

    选择类排序包括: (1)  简单选择排序 (2)树形选择排序 (3)堆排序 简单选择排序: [算法思想]:在第 i 趟简单选择排序中,从第 i 个记录开始,通过 n - i 次关键字比较,从 n - ...