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 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
随机推荐
- 171. Excel Sheet Column Number (Math)
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- 42 【docker】run命令
最常用的两个option是,网络端口映射,和文件共享 最基本的启动命令(从image创建一个container并启动):docker run -d <image-name> -d:表示守护 ...
- 关于php条形码生成(barcode),修改样式
今天听错了需求,以为要重新设计条形码,第一次制作这个,经过搜索使用的barcode这个第三方的,具体使用步骤网上很多就不在这里详细介绍了.主要是今天遇到的样式修改问题: barcode经过查看是无法自 ...
- 20172325 2018-2019-2 《Java程序设计》第九周学习总结
20172325 2018-2019-2 <Java程序设计>第九周学习总结 教材学习内容总结 图的定义 图是由顶点集(VertexSet)和边集(EdgeSet)组成,针对图G,顶点集和 ...
- [费用流][BZOJ1070]修车
修车 题目描述 同一时刻有位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均 ...
- 2019.02.26 bzoj4311: 向量(线段树分治+凸包)
传送门 题意: 支持插入一个向量,删去某一个现有的向量,查询现有的所有向量与给出的一个向量的点积的最大值. 思路: 考虑线段树分治. 先对于每个向量处理出其有效时间放到线段树上面,然后考虑查询:对于两 ...
- C++获取工程路径、exe路径
编码过程中有时候会用到获取工程所在路径或者exe所在的路径信息,这里稍微记录下. 获取工程路径 char pBuf[MAX_PATH]; //存放路径的变量 GetCurrentDirectory(M ...
- ABP框架系列之六:(Value-Objects-值对象)
Introduction "An object that represents a descriptive aspect of the domain with no conceptual i ...
- 【git 报错】Could not read from remote repository.Please make sure you have the correct access rights.
我们在使用git clone 或其他命令的时候,有时候会遇到这类问题,如图: and the repository exists. fatal: Could not read from remote ...
- UBUNTU14.0.4安装eclipse
jdk工具下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 点击这个下载 ...