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”,
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);
额外要求:
- 单词之间最多只能有一个空格,多余空格要删除;
- 字符串首尾不能添加多余空格;
方法一:此题的一个简单的解法,就是,我们可以借助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
}
};
LeetCode(151) Reverse Words in a String的更多相关文章
- LeetCode(7)Reverse Integer
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...
- LeetCode(47)-Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- LeetCode(206) Reverse Linked List
题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...
- LeetCode(190) Reverse Bits
题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- 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- ...
- 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. ...
- 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 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- Leetcode(7)整数反转
Leetcode(6)Z字形变换 [题目表述]: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 第一次:转字符串处理 执行用时:40 ms: 内存消耗:11.6MB 效果: ...
随机推荐
- github新手指南
- 不同ORM新的理解
对于ORM你怎么理解?你用过的ORM有什么区别?这是面试的时候基本上会问的问题. 问题很简单,本文不在阐述.本文主要讨论Dapper 和 EF Core First的区别. 从直观上来看两个都是ORM ...
- get_user
Name get_user -- Get a simple variable from user space. Synopsis get_user ( x, ptr); Arguments x ...
- 在浏览器里使用SAPGUI
事务码SICF,service name输入WEBGUI, 点右键,选择Test Service: 可以在浏览器里敲SE38进入ABAP editor了: 然么缺乏语法高亮显示: 如果想要浏览器里的语 ...
- ABC3D创客项目:风力小车
随着互联网.开源硬件.电子信息等技术成熟应用,以及创新教育的大力普及,创新正成为青少年生活中最热门的话题之一:尤其新兴的3D打印技术将创意者.生产者.消费者合三为一,成为创新教育的又一大助力,每个学生 ...
- 个人作业-Alpha项目测试
姓名 蒋东航 学号 201731062328 这个作业属于哪个课程 课程链接 这个作业要求在哪里 作业要求链接 团队名称 机你太美(团队博客链接) 这个作业的目标 了解其他团队项目,学习其他团队优秀方 ...
- Python 继承实现的原理(继承顺序)
继承顺序 Python3 : 新式类的查找顺序:广度优先 新式类的继承: class A(object): Python2 3 都是了 MRO算法--生成一个列表保存继承顺序表 不找到底部 Pytho ...
- kubernetes-核心概念及创建应用(六)
kubernetes是什么: •Kubernetes是Google在2014年开源的一个容器集群管理系统,Kubernetes简称K8S.•K8S用于容器化应用程序的部署,扩展和管理.•K8S提供了容 ...
- CPP-基础:虚函数表
虚函数表 对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)来实现的.简称为V-Table.在这个表中,主是要一个类的虚函数的地址表 ...
- Python-DDT实现接口自动化
Get请求参数化例子 import unittest import requests import ddt @ddt.ddt class MyTestCase(unittest.TestCase): ...