LeetCode151: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
".
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.
实现代码:
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <sstream>
using namespace std; /* Given an input string, reverse the string word by word. For example,
Given s = "the sky is blue",
return "blue is sky the". 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.
*/ class Solution {
public:
void reverseWords(string &s)
{ reverse(s.begin(), s.end());
string::iterator p_iter = s.begin();
string::iterator q_iter; //去除最前面和最后面的空白符
int index = s.find_first_not_of(' ');
if(index == string::npos)//如果字符串全为空白符
{
s.clear();
return;
}
s.erase(s.begin(), s.begin()+index);
int index2 = s.find_last_not_of(' ');
s.erase(s.begin()+index2+, s.end()); p_iter = s.begin();
while(p_iter != s.end())
{
q_iter = p_iter;
while(*p_iter != ' ' && p_iter != s.end()) p_iter++;
reverse(q_iter, p_iter);
bool first = true;
while(*p_iter == ' ' && p_iter != s.end())//去掉单词间多个空白符,只留下一个
{
if(first)
{
p_iter++;
first = false;
}
else
p_iter = s.erase(p_iter);
} }
} }; int main(void)
{
string str = " this is zhou ";
Solution solution;
solution.reverseWords(str);
cout<<str<<endl;
return ;
}
LeetCode151:Reverse Words in a String的更多相关文章
- leetcode解题报告(25):Reverse Words in a String III
描述 Given a string, you need to reverse the order of characters in each word within a sentence while ...
- lintcode :Reverse Words in a String 翻转字符串
题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" ...
- LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...
随机推荐
- hibernate经常报的几个不起眼的错误, 都是因为不细心或者手滑了输入有误造成了
最近会经常用到hibernate了, 也经常报错, 看着屏幕上一根大红条是非常不爽的, 这几天集合了一下经常报的错误, 整合一下放到这里, 以后再出现这个错误直接去解决就好了 1, org.hiber ...
- LuoguP1032 字符变换(BFS)
题目链接为:https://www.luogu.org/problemnew/show/P1032 思路:看到数据比较小,而且最多有6个规则,就可以用搜索去做了,我用的BFS,大体思路如下: 定义结构 ...
- maven不存在jar包解决
win7环境 下载:https://maven.apache.org/download.cgi 提取文件,并cmd 转到bin目录 假设要添加的jar包是jbarcode-0.2.8.jar, 可执行 ...
- centos7装NVIDIA显卡驱动
一.系统及显卡 系统:centos7.5 64位 显卡:gtx 1060 前几天主要是有一个人脸识别的项目测试,需要用到显卡去测试性能,然后装显卡的过程折腾了一下,特此记录. 二.安装过程 1. 下载 ...
- ios - 设置一个VC的navigationController的显示图案或文字,其他navigationController依旧不变
1. override func viewDidLoad() { super.viewDidLoad() self.navigationController?.delegate = self } 2. ...
- 04 存储库之mongodb
MongoDB 一 简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库1.易用性 MongoDB是一个面向文档(document-oriented)的数据库,而不是关系型数据库.不采用 ...
- part1:7-Linux网络配置
1.虚拟机(Vmware)网络配置 VMware虚拟机对于不同的网络环境提供了三种网卡工作模式: Bridged:网桥模式: 在桥接模式下,计算机A充当路由器与虚拟机之间的“桥”,虚拟机通过计算机A的 ...
- myeclipse 中 svn 更新 提交 同步资源库 详细解释下他们的功能
原理是这样的 svn服务器一般放在公共的服务器上,大家连这个服务器,在MyEclipse上使用svn控件 可以下载svn上的项目至本地,所以很多公司将开发要用到的软件都放在svn上,有同事来只要连上s ...
- 20155335俞昆《java程序设计》第七周总结
学号 2016-2017-2 <Java程序设计>第X周学习总结 ## 教材学习内容总结 Lambda 的语法概览 String[] names={“Justin”,”cater ...
- 2018.09.25 poj3070 Fibonacci(矩阵快速幂)
传送门 矩阵快速幂板题,写一道来练练手. 这一次在poj做题总算没忘了改万能库. 代码: #include<iostream> #include<cstdio> #define ...