4.Reverse Words in a String-Leetcode
class Solution {
public:
void reverseWords(string &s) {
vector<string> data;
string word;
stringstream ss(s);
while(ss>>word) data.push_back(word);
vector<string> rdata(data.rbegin(), data.rend());
s = accumulate(rdata.begin(), rdata.end(), string(""),
[](string s1, string s2){
if(s1.empty()) return s2;
else return s1+" "+s2;
});
}
};
class Solution {
public:
void reverseWords(string &s) {
for(string::size_type ix=0;ix!=s.size();++ix)
{
if(s[ix]==' ')
if(s[ix+1]==' '){
s.erase(ix,1);
ix--;
}
}
if(s[0]==' ')s.erase(0,1);
if(s[s.size()-1]==' ')s.erase(s.size()-1,1);
int k=0;
string st(s.size(),'a');
for(string::size_type ix=s.size()-1;ix!=-1;--ix)//全倒置
{
st[k++]=s[ix];
}
int beg=0,end=0,n=st.size();
int index=0;
while(end<=n)
{
while(st[end]!=' '&&end<n)end++;
for(int i=end-1;i>=beg;--i)s[index++]=st[i];
if(st[end]==' ')s[index++]=' ';
end=end+1;
beg=end;
}
}
};
4.Reverse Words in a String-Leetcode的更多相关文章
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- Reverse Words in a String——LeetCode
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Reverse Words in a String leetcode
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Reverse Words in a String leetcode java
题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
- Reverse Words in a String | LeetCode OJ | C++
我的思路:先读取每一个单词,存放到容器中:读取完毕后,将容器中的单词倒序写入输出中. #include<iostream> #include<string> #include& ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
随机推荐
- Spring:面向切面编程的AOP
一.前言 除了依赖注入(DI),Spring框架提供的另一个核心功能是对面向方面的编程(AOP)的支持. AOP通常被称为实现横切关注点的工具.横切关注点一词是指应用程序中的逻辑不能与应用程序的其余部 ...
- Linux下文件的三种时间标记:访问时间、修改时间、状态改动时间 (转载)
在windows下,一个文件有:创建时间.修改时间.访问时间. 而在Linux下,一个文件也有三种时间,分别是:访问时间.修改时间.状态改动时间. 两者有此不同,在Linux下没有创建时间的概念,也就 ...
- linux exit 和 _exit的区别
今天仔细看了一下exit和_exit这两个函数的区别,实际上exit也是调用了_exit退出函数的,只不过在调用_exit之前,exit还进行了一些多余的工作,也正是因为这样,相比起来exit就没有那 ...
- POJ 3692 Kindergarten(二分图最大独立集)
题意: 有G个女孩,B个男孩.女孩彼此互相认识,男孩也彼此互相认识.有M对男孩和女孩是认识的.分别是(g1,b1),.....(gm,bm). 现在老师要在这G+B个小孩中挑出一些人,条件是这些人都互 ...
- IDEA升级开源框架
在开发过程中,我们经常会用到一些 GitHub或者Gitee上的开源框架来快速搭建我们的业务系统,但是当框架被我们大批量修改后,开源框架又有升级了.这时候升级框架就变得很麻烦,也不能直接直接进行合并, ...
- 从0到1搭建自己的组件(vue-code-view)库(上)
0x00 前言 本文将从结构.功能等方面讲解下项目 vue-code-view 的搭建过程,您可以了解以下内容: 使用 vue cli 4从0搭建一个组件库及细致配置信息. 项目的多环境构建配置. 项 ...
- MySQL 默认隔离级别是RR,为什么阿里这种大厂会改成RC?
我之前写过一篇文章<为什么MySQL选择REPEATABLE READ作为默认隔离级别?>介绍过MySQL 的默认隔离级别是 Repeatable Reads以及背后的原因. 主要是因为M ...
- charles抓包修改请求参数发送新的请求
打开charles -->选择请求右击选择compose---修改参数发送请求
- java 获得 微信 UserId
.... public String cs() throws Exception{ /*访问页面,服务器会得到 code(request.getParameter("code")) ...
- [atAGC020E]Encoding Subsets
令$f_{S}$表示字符串$S$的答案(所有子集的方案数之和),考虑转移: 1.最后是一个字符串,不妨仅考虑最后一个字符,即$f_{S[1,|S|)}$(字符串下标从1开始),特别的,若$S_{|S| ...