题目: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. HALCON示例:BOTTLE.HDEV 光学字符识别(分割OCR)

    * * bottle.hdev: Segment and read numbers on a beer bottle 分割读取啤酒瓶上的数字* * Step 0: Preparations* Spec ...

  2. Willem, Chtholly and Seniorious

    Willem, Chtholly and Seniorious https://codeforces.com/contest/897/problem/E time limit per test 2 s ...

  3. FortiGate 路由

    1.静态路由 防火墙外网口wan1 ip地址为202.1.1.2,对端ISP路由器G1/0口地址为202.1.1.1. 菜单: 路由--静态--静态路由,点击 "创建新的",按如下 ...

  4. 每10秒执行定时任务-crontab

    * * * * * /data/crontab.sh * * * * * sleep 10; /data/crontab.sh * * * * * sleep 20; /data/crontab.sh ...

  5. php Pthread 多线程 Worker

    <?php //PHP 高级编程之多线程 http://www.netkiller.cn/journal/thread.php.html#idp57489856 //worker 是一个具有持久 ...

  6. tornado框架学习及借用有道翻译api做自动翻译页面

    趁着这几天有时间,就简单的学了一下tornado框架,简单做了个自动翻译的页面 仅为自己学习参考,不作其他用途 文件夹目录结构如下: . ├── server.py ├── static │   └─ ...

  7. redis使用规范文档 20170522版

    运维redis很久了,一直是口头给rd说各种要求,尝试把这些规范总结成文档 摘选一些可能比较通用的规则如下: 强制:所有的key设置过期时间(最长可设置过期时间10天,如有特殊要求,联系dba说明原因 ...

  8. linux下反弹shell

    01 前言 CTF中一些命令执行的题目需要反弹shell,于是solo一波. 02 环境 win10      192.168.43.151       监听端    装有nc kali        ...

  9. 模板学习实践一 accumulationtraits

    // 11111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  10. Delphi 域名解析为IP地址

    花生壳:1.LJSZForm-Lable1-Caption改成 “IP地址或域名:”2.LJSZForm-BitBtn1Click-注释掉--else if IsIP(Trim(IPEdit.Text ...