题目

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. webRTC 基础介绍

    WebRTC 全称为:Web Real-Time Communication.它是为了解决 Web 端无法捕获音视频的能力,并且提供了 peer-to-peer(就是浏览器间)的视频交互.实际上,细分 ...

  2. Oracle 11g R2手动配置EM(转)

    转自:http://blog.itpub.net/9034054/viewspace-1973418/ Oracle 11g R2手动配置EM Oracle 作者:luashin 时间:2016-01 ...

  3. Fiddler抓包_次要功能和第三方插件

    1.替换HTTP Request Host 应用场景:进行开发时,线上去测试跳转调试 替换命令:urlreplace news.baidu.com www.baidu.com: 清除命令:urlrep ...

  4. 查找算法(7)--Hash search--哈希查找

    1.哈希查找 (1)什么是哈希表(Hash) 我们使用一个下标范围比较大的数组来存储元素.可以设计一个函数(哈希函数, 也叫做散列函数),使得每个元素的关键字都与一个函数值(即数组下标)相对应,于是用 ...

  5. Visual-Based Autonomous Driving Deployment from a Stochastic and Uncertainty-Aware Perspective

    张宁 Visual-Based Autonomous Driving Deployment from a Stochastic and Uncertainty-Aware Perspective Le ...

  6. 设置驱动的方法(Chrome 亲测ok)

    驱动下载地址 http://selenium-release.storage.googleapis.com/index.html package com.selenium.java.webdriver ...

  7. requests库学习案例

    requests库使用流程 使用流程/编码流程 1.指定url 2.基于requests模块发起请求 3.获取响应对象中的数据值 4.持久化存储 分析案例 需求:爬取搜狗首页的页面数据 # 爬取搜狗首 ...

  8. Java分布式:分布式服务框架——ZooKeeper

    Java分布式:ZooKeeper——核心概念 ZooKeeper 统一配置管理 统一命名服务 分布式锁

  9. 【视频开发】ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)

    转载地址:http://blog.csdn.net/gubenpeiyuan/article/details/25618177 概要:           目前ONVIF协议家族设备已占据数字监控行业 ...

  10. matlab柱状图画法

    %%各时段电量需求 clc close all clear all x = [11000 33000 25000 36000 25000 30000 18000]; tick = {'0-6' '6- ...