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 效果: ...
随机推荐
- Entitas实现简析
Entitas实现简析 这里主要讲Entitas的执行原理,不讲Entitas的代码生成方面. ECS简介 ECS(实体-组件-系统)是一种常用于游戏开发的架构模式. 实体: 实体只是一个 ...
- Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率和决策树多元分类使用.precision方法以precision来评估模型的准确率(图文详解)
不多说,直接上干货! Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的 ...
- 常用的http网页错误代码表---------495引发的一个简单到爆,但基于国内环境只能呵呵呵的血案
敲代码敲出了个网页错误代码 495. 然后,正常的跑去百度,看了一堆还是没有完整的网页错误代码,应该说国内的环境的网页错误代码表只有官方的那几个,那么只能FQ了. 去到谷歌,一查全是俄语,乐了,明白是 ...
- App配置页面头部
记录一下 App配置页面头部 例 上图红色框部分就是自己配置的头部 //我的客户 "/OACustomer/Index": { title: "我的客户", h ...
- JAVA 员工管理系统(用抽象类实现),简易版。
package Demo513; /* 定义一个Employee类,该类包含: private 成员变量name,number,birthday,其中birthday为MyDate类的对象: abst ...
- 洛谷 P1137 旅行计划
旅行计划 待证明这样dp的正确性. #include <iostream> #include <cstdio> #include <cstring> #includ ...
- 3285 转圈游戏 2013年NOIP全国联赛提高组
3285 转圈游戏 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description n 个小伙伴 ...
- Arduino ESP8266编程深入要点
Arduino for ESP8266的话,如果不修改代码,默认没有办法进入轻睡眠的省电模式,只能进入Modem Sleep,也就是说Wifi可以暂时睡眠但是CPU没法睡,Modem Sleep最低功 ...
- ptxas fatal : Unresolved extern function Error 255
This question already has an answer here: External calls are not supported - CUDA 1 answer I am tryi ...
- MySQL++简单使用记录.md
#1.简介 MySQL++ is a powerful C++ wrapper for MySQL’s C API. Its purpose is to make working with queri ...