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. java websocket中的ping-pong 机制

    参考源码: https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/cli ...

  2. Reboot

    目标是将浏览器的预设样式设为一致 Native font stack  本机字体堆栈 由于padding 及 border 会改变元素在运算后的宽度 此时的实际宽度为: width+左右padding ...

  3. Java web错误汇总

    环境: 电脑:win 10 IDE: Eclipse Java EE IDE for Web Developers. Version: Luna Service Release 2 (4.4.2) B ...

  4. [CodeChef] The Street

    给定两个长度为n的数列A和B,开始数组A中每一项值为0,数组B中每一项值为负无穷大.接下来有m次操作:1.数组A区间加一个等差数列:2.数组B区间对一个等差数列取max:3.询问ai+bi的值.n&l ...

  5. 洛谷P4332 [SHOI2014]三叉神经树(LCT)

    传送门 FlashHu大佬太强啦%%% 首先,我们可以根据每一个点的权值为$1$的儿子的个数把每个点记为$0~3$,表示这一个点的点权 先考虑一下暴力的过程,假设从$0$变为$1$,先更改一个叶子结点 ...

  6. bzoj1934: [Shoi2007]Vote 善意的投票(最小割)

    传送门 考虑源点为同意,汇点为反对,那么只要源点向同意的连边,不同意的向汇点连边,求个最小割就是答案 然后考虑朋友之间怎么办,我们令朋友之间连双向边.这样不管怎么割都能对应一种选择情况.那么还是求一个 ...

  7. [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标

    目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...

  8. IOS Swift UITableViewcontroller实现点击空白处隐藏键盘

    在ios开发中,为了方便,我们经常使用UITableViewcontroller,比如搜索界面为了方便可能更多的使用UITableViewcontroller,那么问题就来了,当我点击搜索框的时候会弹 ...

  9. C++基本变量类型

    算数类型表 类型 含义 最小存储空间 取值范围 bool 布尔型 –   char 字符型 8位 -2^7 ~ 2^7-1 wchar_t 宽字符型 16位   short 短整型 16位 -2^15 ...

  10. vue2中使用ajax

    vue中本身没有ajax功能,需要使用扩展,现在推荐使用的是axios,github地址如下 https://github.com/axios/axios 下面各个示例 <!DOCTYPE ht ...