LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium
题目: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的更多相关文章
- [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 ...
- String 字符串中含有 Unicode 编码时,转为UTF-8
1.单纯的Unicode 转码 String a = "\u53ef\u4ee5\u6ce8\u518c"; a = new String(a.getBytes("UTF ...
- 将string字符串中的换行符进行替换
/** * 方法名称:replaceBlank * 方法描述: 将string字符串中的换行符进行替换为"" * */ public static String replaceBl ...
- 字符串中单词的逆转,即将单词出现的顺序进行逆转。如将“Today is Friday!”逆转为“Friday! is Today”.
字符串中单词的逆转,即将单词出现的顺序进行逆转.如将“Today is Friday!”逆转为“Friday! is Today”. #include<iostream> #include ...
- C语言:将字符串中的字符逆序输出,但不改变字符串中的内容。-在main函数中将多次调用fun函数,每调用一次,输出链表尾部结点中的数据,并释放该结点,使链表缩短。
//将字符串中的字符逆序输出,但不改变字符串中的内容. #include <stdio.h> /************found************/ void fun (char ...
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- [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 ...
- C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
随机推荐
- 数据库启动windows
1.上 MongoDB官网下载数据库,下载之后选择自己想放的文件夹要记住文件夹位置,比如我下载之后就放在D盘,改文件夹为 mongodb 2.启动之前要给mongodb指定一个文件夹,这里取名为&qu ...
- springmvc shiro UnauthorizedException 异常解决方案
springMVC 整合 shiro 时,配置了当访问某个URL没有权限时的配置处理: <!-- 通过unauthorizedUrl指定没有权限操作时跳转页面 --><propert ...
- Numpy三维数组的转置与交换轴
二维数组的转置应该都知道,就是行列交换 而在numpy中也可以对三维数组进行转置,np.T 默认进行的操作是将0轴与2轴交换 本文主要对三位数组轴交换的理解上发表本人的看法. a = np.array ...
- java中的throw、throws和try catch浅析
今天在公司和同事聊天的时候,突然发现自己对java中的throw.throws和try catch的作用理解不够准确,在网上查了查,在此大概梳理一下. throw用于抛出异常,例如 throw new ...
- HDU-1260.Tickets(简单线性DP)
本题大意:排队排票,每个人只能自己单独购买或者和后面的人一起购买,给出k个人单独购买和合买所花费的时间,让你计算出k个人总共花费的时间,然后再稍作处理就可得到答案,具体格式看题意. 本题思路:简单dp ...
- ExecuteNonQuery()
ExecuteNonQuery():执行一个SQL语句,返回受影响的行数,这个方法主要用于执行对数据库执行增加.更新.删除操作,注意查询的时候不是调用这个方法.用于完成insert,delete,up ...
- c#mysql批量更新的两种方法
总体而言update 更新上传速度还是慢. 1: 简单的insert 速度稍稍比MySqlDataAdapter慢一点 配合dapper 配置文件 <?xml version="1 ...
- python历史与基本类型
前言 我自学的方式主要是看文档,看视频,第一次做写博客这么神圣的事情,内心是忐忑的,写的东西比较杂,路过的小伙伴不要嘲笑我,主要是记录一日所学,顺便锻炼一下语言组织能力吧,anyway,这些都不重要, ...
- surf the internet scientifically
You can get some useful information from the link below: 1. http://blog.sina.com.cn/s/blog_4891cbc50 ...
- ssms创建链接服务器