题目

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. Xamarin.FormsShell基础教程(7)Shell项目关于页面的介绍

    Xamarin.FormsShell基础教程(7)Shell项目关于页面的介绍 轻拍标签栏中的About标签,进入关于页面,如图1.8和图1.9所示.它是对应用程序介绍的页面. 该页面源自Views文 ...

  2. MQTT研究之EMQ:【EMQX使用中的一些问题记录(1)】

    issue 1. EMQX的共享订阅 EMQX是一个非常强大的物联网通信消息总线,基于EMQX开展应用开发,要注意很多配置细节问题,这里要说到的就是共享订阅以及和cleanSession之间的关系问题 ...

  3. spring与springMVC的父子容器关系

    背景和概述 在spring与springMVC中通过IOC可以管理bean对象,有两个配置文件可以配置ioc spring的配置文件applicationContext.xmlspringMVC的配置 ...

  4. SpringCloud基础

    SpringCloud极大的简化了分布式系统的开发,实现了微服务的快速部署和灵活应用 SpringCloud主要框架 * 服务发现--Netfix Eureka * 服务调用--Netfix Feig ...

  5. [转] 2017年PHP开发者大会 鸟哥 (惠新宸@Laruence)精彩问答

    php7.1那个诡异的函数返回类型限定是如何考虑的? 鸟哥:没什么特别考虑,投票投出来的.首先说明一点,我投的是反对票.包括php的命名空间反斜杠我也是非常反对的,但可能由于我并没有对这方面太深的认识 ...

  6. Redis解决“重试次数”场景的实现思路

    很多地方都要用到重试次数限制,不然就会被暴力破解.比如登录密码. 下面不是完整代码,只是伪代码,提供一个思路. 第一种(先声明,这样写有个bug) import java.text.MessageFo ...

  7. 洛谷--P3808 【模板】AC自动机(“假的“简单版)

    如果你想要做出这道题,你需要先了解两个知识点: 1.字典树的构造 2.KMP算法(也就是fail指针的构造) 对于字典树,可以看看这个大佬: https://www.cnblogs.com/TheRo ...

  8. 通过fsockopen()方法从中国福彩网获取双色球历史中奖数据

    # 以下代码基于 CI 框架 # public function history_draw($page = 0) { set_time_limit(0); $page++; $up2now = dat ...

  9. Python之路【第二十九篇】:django ORM模型层

    ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...

  10. golang module 下载外网资源失败解决办法

    用 golang 1.11 module 特性时,需要下载golang.org等外网地址的库文件 可以创建环境变量GOPROXY,使用Aliyun的镜像 go公共代理文档 简介 go module公共 ...