博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Reverse Words in a String
阅读量:7028 次
发布时间:2019-06-28

本文共 1245 字,大约阅读时间需要 4 分钟。

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.
提交了好多遍,终于成功了,使用了sstream来分割字符串,使用栈来存放分割之后的字符串,然后出栈就是逆序了。。
 
C++实现代码:
#include
#include
#include
#include
using namespace std;class Solution{public: void reverseWords(string &s) { if(s.empty()) return; stringstream ss(s); s.clear(); stack
st; string tmp; while(ss>>tmp) { st.push(tmp); } while(!st.empty()) { tmp=st.top(); st.pop(); if(!st.empty()) s+=tmp+' '; else s+=tmp; } }};int main(){ Solution s; string ss=" the sky is blue "; cout<
<

运行结果:

运行结果:

 

转载地址:http://bcrxl.baihongyu.com/

你可能感兴趣的文章
使用 Hexo 创建项目文档网站
查看>>
typeof和instanceof的区别
查看>>
XAMPP Windows 安装中报错解决方法备忘
查看>>
sublime之利器使用篇
查看>>
每个类都应将所有能力以最小粒度提供给外部可配置,每个业务所需要的功能是这些能力的组合...
查看>>
使用cached的wrapper类读取请求响应内容
查看>>
[python][os]分离文件目录,文件名以及文件后缀
查看>>
解决Android Studio SDK无法下载问题
查看>>
雷军定AI+IoT为小米核心战略,牵手宜家推进生态布局
查看>>
书评:《All About Java 8 Lambdas》
查看>>
搜狗信息流推荐算法实践
查看>>
Visual Studio 2017 15.6发布
查看>>
2019年Java和JVM生态系统预测:OpenJDK将成为Java运行时市场领导者
查看>>
拥抱PostgreSQL,红帽再表态:SSPL的MongoDB坚决不用
查看>>
架构设计复杂度的6个来源
查看>>
360首席安全官谭晓生宣布离职
查看>>
在敏捷中应用测试驱动开发
查看>>
到底谁应该对软件开发的质量负责?
查看>>
微软Windows Core OS被曝应用了开源组件
查看>>
用Elm语言降低失败的风险
查看>>