题目: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. Python实现EXCEL表格的排序功能

    EXCEL的数值排序功能还是挺强大的,升序.降序,尤其自定义排序,能够对多个字段进行排序工作. 那么,在Python大法中,有没有这样强大的排序功能呢?答案是有的,而且本人觉得Python的排序功能, ...

  2. Truthy真值

    在 JavaScript 中,Truthy (真值)指的是在 布尔值 上下文中转换后的值为真的值.所有值都是真值,除非它们被定义为 falsy (即除了 false,0,"",nu ...

  3. elasticsearch5安装

    环境: centos7 es 5.4.3 es安装 一.下载 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsear ...

  4. easyui 回车搜索

    代码: js: // 搜索 $("#searchButton").click(function() { var ip = $("#searchIp").val( ...

  5. 796. Rotate String旋转字符串

    [抄题]: We are given two strings, A and B. A shift on A consists of taking string A and moving the lef ...

  6. 利用travis自动化构建与部署(文档项目)

    背景 保持网站上文档的最新性有比较重要的意义, travis ci 提供了免费的解决方案,本文基于 latex 构建+ aliyun oss 部署对此作了尝试. 项目链接为 https://travi ...

  7. java_16Arrays类

    1sort():对数组进行升序排列 public static void main(String[] args) { int[] arr= {2,43,6,7}; Arrays.sort(arr); ...

  8. 通过Solr所提供的Dataimporthandler实现数据源的导入

    如需要使用到Solr中的dataimporthandler增量导入功能,则还需要引入两个所依赖的jar包,在上一篇随笔中所提到的下载的Solr项目文件solr-4.10.3\dist目录下可以找到所依 ...

  9. 更改h标签的字体粗细

    h1,h2,h3,h4,h5,h6{ font-weight:normal }

  10. C#语言不常用语法笔记

    只看过3天C#语法书,了解个大概,与C++等不同之处,或者看开源遇到一些奇异用法,记录一下,脑子不够用的情况下,还是记笔记靠谱. ==================== 顺便吐槽下,这年头得会各种编 ...