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. SQL Server 查询分析器提供的所有快捷方式(快捷键)

    SQL Server程序员经常要在SSMS(SQL Server Management Studio)或查询分析器(2000以前)中编写T-SQL代码.以下几个技巧,可以提升工作效率. 以下说明以SS ...

  2. Boost Python官方样例(三)

    导出C++类(纯虚函数和虚函数) 大致做法就是为class写一个warp,通过get_override方法检测虚函数是否被重载了,如果被重载了调用重载函数,否则调用自身实现,最后导出的时候直接导出wa ...

  3. CentOS6.5 Cloud-Init使用

    使用cloud-init实现虚拟机信息管理 http://blog.marvelworld.tk/?p=575 谈谈Openstack的CentOS镜像 http://www.chenshake.co ...

  4. [CentOS7] at, bash, cron, anacron

    声明:本文主要总结自:鸟哥的Linux私房菜-第十五章.例行性工作排程(crontab),如有侵权,请通知博主 at => /var/spool/at /etc/at.allow, /etc/a ...

  5. HTML 代码格式

     <code> 元素不保留多余的空格和折行: 如需解决该问题,您必须在 <pre> 元素中包围代码:           

  6. Web——页面“传值、赋值”与页间“传值”

    前言 前期我们学习C/S开发的时候,采用三层架构,U层的参数在每个层之间传来传去,那么现在我们接触到的B/S,在Web前端是如何与后台进行传值赋值呢?由于本人是菜鸟一枚,下面写的不恰当的地方,望路过的 ...

  7. 关于SqlDataReader使用的一点疑惑

    C#中的SqlDataReader类(System.Data.SqlClient)是用来在保持打开数据库连接的状态下取数据用的 用法如下图: “保持与数据库的连接”这个特性也是SqlDataReade ...

  8. 关于特殊目录如com null无法删除的处理办法

    1.把以下批处理做成一个del.bat放在桌面 del /f /a /q \\?\%1 rd /s /q \\?\%1 2.需要删除的文件或者文件夹拉到这个文件上就可以了 可以删除没有属性项目中没有安 ...

  9. 单机部署zookeeper、kafka

    先安装jdk:mkdir /usr/javatar xf jdk-8u151-linux-x64.tar.gzmv jdk1.8.0_151/ /usr/java/jdk1.8 cat /etc/pr ...

  10. Idea设置签名

    IntelliJ IDEA如何设置头注释,自定义author和date   下面这张图,保证你一看就会: 下面这个模板,你拿去改一改就行了. 1 /** 2 * @Author: Gosin 3 * ...