class Solution {
public:
void reverseWords(string &s) {
string end="",tem="";
char *p=&s[];
while(*p!='\0'){
while(*p==' ') //过滤多余的空格,针对串头
p++;
while(*p!=' '&&*p!='\0'){ //积累一个单词,存于临时串
tem=tem+*p;
p++;
}
while(*p==' ') //过滤多余的空格,针对串尾
p++;
if(*p!='\0') //最后一个字不用加空格
tem=' '+tem;
end=tem+end;
tem=""; //临时字符串清空
}
s=end;
}
};

题意:将字符串中的字按反序排列,每个字中间有一个空格,串前和串尾无空格。字的顺序不用改变,改变的是字在串中的顺序。

思路:过滤串的前面和后面的空格,用指针从前往后扫, 再用一个临时串保存字,满一个字的时候就添加在将最终的串的前面。扫完该串就将最终的串赋给s。

LeetCode Reverse Words in a String 将串中的字翻转的更多相关文章

  1. leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)

    题目例如以下: Given an input string, reverse the string word by word. For example, Given s = "the sky ...

  2. 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 ...

  3. [leetcode] Reverse Words in a String [1]

    一.题目: Given an input string, reverse the string word by word. For example, Given s = "the sky i ...

  4. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  5. [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 ...

  6. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  7. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  8. [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 ...

  9. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

随机推荐

  1. 释放License命令

    lmutil_x86.exe lmremove -c 1055@Vappcloud-WinS acfd Administrator VappCloud_Win7a.vancloud-corp.com ...

  2. MATLAB求微分

    求微分 diff(函数) , 求的一阶导数;diff(函数, n) , 求的n阶导数(n是具体整数);diff(函数,变量名), 求对的偏导数;diff(函数, 变量名,n) ,求对的n阶偏导数; & ...

  3. 初识SVN

    前言 我们都知道每一件工具的诞生都是为了方便我们的生活.SVN(Subversion)学习工具在我们"合作"开发软件过程中起到了很大的作用.说起SVN先说说SCM. 内容 SCM ...

  4. nodejs + webpack4 + babel6 结合写Chrome浏览器插件记录

    最近任务不忙,有时间了看一下Chrome插件相关的东西,于是想用nodejs + webpack写一个能直出插件的小工具. 1.nodejs + babel6 + webpack4 在写之前,是有把它 ...

  5. JSON长度

    import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Predicate; import net.minidev.json.J ...

  6. ubuntu 的 python从2.* 升级到3.*

    1.在ubuntu 的终端中用代码下载最新的Python sudo apt-get install python3 2.用命令删除usr/bin/目录下的默认python link文件root@sal ...

  7. js:数组里面获取键名和键值

    在写php时用ajax异步传回的返回数组时是json格式,在js里面处理时有时需要用到键名,此时可以用in来处理 js只有数字索引: <script> var data = new arr ...

  8. jquery——尺寸

    1. 获取和设置元素的尺寸 2. 获取元素相对页面的绝对位置:offset() 这种方式增加的盒子不会对之前的结构产生影响 demo: <!DOCTYPE html> <html l ...

  9. 如何创建width与height比例固定的元素

    面试题,刚在github上看到的,说说这里面的知识点吧~~ padding-bottom的值,其百分比是根据元素自身的width来算的. padding,在标准盒模型中,width+padding+b ...

  10. ElasticSearch 全文检索— ElasticSearch 安装部署

    ElasticSearch 规划-集群规划 ElasticSearch 规划-集群规划 ElasticSearch 规划-用户规划 ElasticSearch 规划-目录规划 ElasticSearc ...