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

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

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.
思路:从头往后扫描字符串,从第一个开始不是空格算起。将单词放入temp中,直到遇到空格,作如下处理,将单词放入result中去,接下来再有单词可以放到上一个单词的前面,另加空格。如此反复,就可以将单词与单词为单位反转了。
class Solution {public:    void reverseWords(string &s) {        int nLen=s.size();        if(nLen<=0)            return;        string temp,result;        int index=0;        while(s[index]==' ')            index++;        for(int i=index;i

 

转载于:https://www.cnblogs.com/awy-blog/p/3830810.html

你可能感兴趣的文章
Unity3D 游戏引擎之实现平面多点触摸(二)
查看>>
【Xilinx-Petalinux学习】-02-建立PetaLinux工程
查看>>
TeX中的引号
查看>>
Python 模块(module)
查看>>
region实现大纲效果
查看>>
day1
查看>>
[No0000B5]C# 类型基础 值类型和引用类型 及其 对象判等 深入研究1
查看>>
AJAX JSONP源码实现(原理解析)
查看>>
Java 表达式解析(非原创)
查看>>
[洛谷P4234]最小差值生成树
查看>>
LiveNVR传统安防摄像机互联网直播-二次开发相关的API接口
查看>>
LiveNVR高性能稳定RTSP、Onvif探测流媒体服务配置通道接入海康、大华等摄像机进行全终端无插件直播...
查看>>
c c++ sizeof
查看>>
Intellij IDEA连接Spark集群
查看>>
最长回文子串解法
查看>>
代码优化程序性能
查看>>
腾讯实习生招聘笔试题目
查看>>
Java Socket编程----通信是这样炼成的
查看>>
作业要求 20180925-1 每周例行报告
查看>>
1078. Hashing (25)-PAT甲级真题
查看>>