题目:Reverse Words in a String

Given an input string, reverse the string word by word. 

For example,
Given s = "the sky is blue",
return "blue is sky the".

比较基础的一个题,拿到这个题,我的第一想法是利用vector来存每一个子串,然后在输出,这是一个比较简单的思路,此外,还有第二个思路,就是对所有的字符反转,然后在针对每一个子串反转,但是这个时候要注意它的要求,保证每个子串中只有一个空格。我是按照第一种思路,代码如下:

 void reverseWords(string &s)
{
int i = , j = ;
string subStr;
vector<string> vecStr;
for (j = ; j != s.length()+; ++j) {
if (s[j] == ' '||j == s.length()) { //Ensure that the final substr can be get
subStr = s.substr(i, j - i);
if (subStr != "") //remove the "" from begin and end str
vecStr.push_back(subStr);
i = j + ;
}
} int vecLen = vecStr.size();
if (vecLen > ) { // deal with the s = ""
string strResult = "";
for (i = vecLen - ; i > ; i --) {
strResult += vecStr[i] + " ";
}
strResult += vecStr[i];
s = strResult;
}
else
s = "";
}

测试情况注意几种:首尾有" "的情况;有多个" "的情况;s = ""的情况;

另外,看到有网友zhangyuehuan的专栏提供了一种更为简洁的思路:

从字符串的最后一个字符遍历,遇到空格就保存子串,然后再对子串反转,和我上面的思路类似,只不过我的遍历方法是正向遍历的,但是其代码简洁,值得学习:

void reverseWords(string & s)
{
string ss;
int i = s.length()-;
while(i>=)
{
while(i>=&&s[i] == ' ') //处理多个空格的情况
{
i --;
}
if(i<) break;
if(ss.length()!=)
ss.push_back(' ');
string temp ;
for(;i>=&&s[i]!=' ';i--)
temp.push_back(s[i]);
reverse(temp.begin(),temp.end());
ss.append(temp);
}
s=ss;
}

LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium的更多相关文章

  1. [LeetCode] Add Bold Tag in String 字符串中增添加粗标签

    Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and ...

  2. String 字符串中含有 Unicode 编码时,转为UTF-8

    1.单纯的Unicode 转码 String a = "\u53ef\u4ee5\u6ce8\u518c"; a = new String(a.getBytes("UTF ...

  3. 将string字符串中的换行符进行替换

    /** * 方法名称:replaceBlank * 方法描述: 将string字符串中的换行符进行替换为"" * */ public static String replaceBl ...

  4. 字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“Today is Friday!”逆转为“Friday! is Today”.

    字符串中单词的逆转,即将单词出现的顺序进行逆转.如将“Today is Friday!”逆转为“Friday! is Today”. #include<iostream> #include ...

  5. C语言:将字符串中的字符逆序输出,但不改变字符串中的内容。-在main函数中将多次调用fun函数,每调用一次,输出链表尾部结点中的数据,并释放该结点,使链表缩短。

    //将字符串中的字符逆序输出,但不改变字符串中的内容. #include <stdio.h> /************found************/ void fun (char ...

  6. [LeetCode] Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  7. [LeetCode] 567. Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  8. C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...

  9. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

随机推荐

  1. py2与py3区别总结

    1. py2中的str是py3中的bytes py2中的Unicode是py3中的str 声明一个字符串变量时,py2 和py3都是str类型,但py2代表字节类型,py3代表文本类型 隐式转换: p ...

  2. IntelliJ IDEA2017 激活方法 最新的(亲测可用)

    IntelliJ IDEA2017 激活方法(亲测可用): 搭建自己的授权服务器,对大佬来说也很简单,我作为菜鸟就不说了,网上有教程. 我主要说第二种,现在,直接写入注册码,是不能成功激活的(如果你成 ...

  3. SAP 图形页面

    话不多说,先上炫图. *&---------------------------------------------------------------------* *& Repor ...

  4. 深入浅出PF 学习笔记---资源文件

    引用   xmlns:sys="clr-namespace:System;assembly=mscorlib" <Window.Resources><sys:St ...

  5. linux环境启动数据库

    1.查看数据库监听的状态: 监听状态:lsnrctl status  出现如下列截图所示数据,说明切切换账户有问题:切换账户时要家:-:  如 su - oracle 第一步:打开Oracle监听$ ...

  6. pythone函数基础(8)内置函数学习

    内置函数学习# sorted# map# filter# max# sum# round# chr# ord# dir# bool# eval# exec# zipimport mathres = m ...

  7. 写jsp文件时需要注意的一些小细节

    ①jsp文件的最开始的部分: <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  8. 50-用Python监听鼠标和键盘事件

    转自:https://www.cnblogs.com/qiernonstop/p/3654021.html 用Python监听鼠标和键盘事件 PyHook是一个基于Python的“钩子”库,主要用于监 ...

  9. 学习笔记《简明python教程》

    学习笔记<简明python教程> 体会:言简意赅,很适合新手入门 2018年3月14日21:45:59 1.global 语句 在不使用 global 语句的情况下,不可能为一个定义于函数 ...

  10. VS中程序包错误,引用错误该如何解决

    1.找到包的文件.packages.config 对应于: 2.删除掉 packages.config 报错的项.然后再重新添加一次.就没有解决的不了的问题. 是不是很爽.....