题目

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.

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.

分析

给定一个字符串,以单词为单位对该字符串进行翻转,要求空间复杂度为O(1);

额外要求:

  1. 单词之间最多只能有一个空格,多余空格要删除;
  2. 字符串首尾不能添加多余空格;

方法一:此题的一个简单的解法,就是,我们可以借助vector,把字符串的单词按序加入容器,然后直接反转容器,再更新元字符串即可,但是此方法不符合空间复杂度的要求。

方法二:经过两步解决,首先,反转整个字符串,然后从前向后遍历,每经过一个单词,反转该单词一次。

当然在过程中,必须合理的处理多余空格问题。详见代码!

AC代码


class Solution {
public:
void reverseWords(string &s) {
if (s.empty())
return; //首先,反转整个字符串
reverse(s.begin(), s.end()); int n = s.size(); //再反转每个单词,同时删除多余的空格,最终的字符串头尾不能是空格,且单词之间不能有多余的空格(最多一个)
string tmp = s;
s.clear();
//标记第一个单词,方便处理单词间的空格
bool flag = true;
for (int i = 0; i < n;)
{
//找到第一个非空格
if (tmp[i] == ' ')
{
++i;
continue;
}//if string::iterator beg = tmp.begin() + i, end = tmp.begin(); for (int j = i; j < n; ++j)
{
//到达一个单词间的空格或者到整个字符串的结束
if (tmp[j] == ' ')
{
end += j; reverse(beg, end);
//链接反转后的第一个单词
if (flag)
{
s = s + tmp.substr(i, j - i);
flag = false;
}
else{
s = s + " " + tmp.substr(i, j - i);
} i = j + 1;
break;
}//if if (j == n - 1)
{
reverse(beg, tmp.end());
if (flag)
{
s = s + tmp.substr(i, j - i + 1);
}
else{
s = s + " " + tmp.substr(i, j - i + 1);
}
i = j + 1;
break;
}
}//for
}//for
}
};

GitHub测试程序源码

LeetCode(151) Reverse Words in a String的更多相关文章

  1. LeetCode(7)Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...

  2. LeetCode(47)-Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  3. LeetCode(206) Reverse Linked List

    题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...

  4. LeetCode(190) Reverse Bits

    题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  5. LeetCode(92) Reverse Linked List II

    题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...

  6. LeetCode(25)Reverse Nodes in k-Group

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  7. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

  9. Leetcode(7)整数反转

    Leetcode(6)Z字形变换 [题目表述]: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 第一次:转字符串处理 执行用时:40 ms: 内存消耗:11.6MB 效果: ...

随机推荐

  1. Jmeter4.0----编写测试脚本(5)

    1.说明 以HTTP请求为例,和小伙伴门分享一下jmeter测试脚本的基本编写步骤 2.步骤说明 第一步:打开jmeter,更改测试计划名称为 Test batchSignForDir(修改计划名称, ...

  2. eShopOnContainers(一)

    微软微服务架构eShopOnContainers(一) 为了推广.Net Core,微软为我们提供了一个开源Demo-eShopOnContainers,这是一个使用Net Core框架开发的,跨平台 ...

  3. NET Core准备:使用Hyper-V安装Ubuntu Server 16.10

    NET Core准备:使用Hyper-V安装Ubuntu Server 16.10 概述 Hyper-V是微软的一款虚拟化产品,和VMWare一样采用的hypervisor技术.它已经被内嵌到Win1 ...

  4. MCS-51单片机的定时器/计数器概念

    一.MCS-51单片机的定时器/计数器概念 单片机中,脉冲计数与时间之间的关系十分密切,每输入一个脉冲,计数器的值就会自动累加1,而花费的时间恰好是1微秒;只要相邻两个计数脉冲之间的时间间隔相等,则计 ...

  5. VS2010/VS2013项目创建及通过ADO.NET连接mysql/sql server步骤(VS2013连接成功步骤见上一篇随笔)

    本随笔主要是对初学者通过ADO.NET连接数据库的步骤(刚开始我也诸多不顺,所以总结下,让初学者熟悉步骤) 1.打开VS新建一个项目(这里的VS版本不限,建项目都是一样的步骤) VS2010版本如图: ...

  6. 一个很好用的侧滑框架ICSDrawerController实现的 QQ 侧滑及换肤功能

    使用ICSDrawerController 实现侧滑功能 在ICSDrawerController 第三方上做了修改实现,QQ 点击头像打开关抽屉头像渐变的效果 - (void)hiddenHeadV ...

  7. 在IDEA中编辑struts国际化properties文件

    在IDEA中编辑struts国际化properties文件 如果手工创建的web工程,struts的i18n属性文件,可以使用native2ascii工具转换(记得命令行的第二个文件名是要保存的文件名 ...

  8. Kendo MVVM 数据绑定(八) Style

    Kendo MVVM 数据绑定(八) Style Style 绑定可以通过 ViewModel 绑定到 DOM 元素 CSS 风格属性,例如: <span data-bind="sty ...

  9. SQL基本语法备忘

    注:以下演示是在mysql命令行下的操作 数据库相关操作 create database mytest; --创建数据库 create database if not exists mytest; - ...

  10. WinForm 窗体

    Winform是.NET开发中对windows Form的一种称谓,form是窗体的意思,winform 称之为windows form. 一般中我们使用的东西分为 客户端.网页.APP 三大类. w ...