当前位置:网站首页>Lesson 3: reverse word order
Lesson 3: reverse word order
2022-07-27 14:08:00 【Fight! Sao Nian!】
subject :AcWing 77. Flip word order
Enter an English sentence , Words are separated by a space , There are no extra spaces at the beginning and end of the sentence .
Turn over the order of the words in the sentence , But the order of the characters in the word is the same .
For the sake of simplicity , Punctuation is treated like ordinary letters .
For example, input string "I am a student.“, The output "student. a am I”.
Data range
Input string length [0,1000].
Examples
Input :
“I am a student.”
Output :
“student. a am I”
Topic analysis :
Method 1 : Open up an extra space ( Time O(n), Space O(n))
Go through the array , If you find a word, you insert the result array with forward interpolation , More trouble
class Solution {
public:
string reverseWords(string s) {
if(s.size()==0)return "";
string res;
string mid;
for(auto it:s)
{
if(it==' ')
{
res=mid+" "+res;
mid="";
}else mid+=it;
}
res=mid+" "+res;
res.pop_back();
return res;
}
};
Method 2 : Two flips ( Time O(n), Space O(1))
We can easily flip the entire string , After flipping , We can get the result by flipping each word
class Solution {
public:
void reversestr(string &s,int l,int r)
{
while(l<r)swap(s[l],s[r]),l++,r--;
}
string reverseWords(string s) {
reversestr(s,0,s.size()-1);
for(int i=0;i<s.size();i++)
{
int j=i;
while(j<s.size()&&s[j]!=' ')j++;
reversestr(s,i,j-1);
i=j;
}
return s;
}
};
边栏推荐
- C#测量工具示意图
- 小程序毕设作品之微信校园洗衣小程序毕业设计成品(2)小程序功能
- Recursive method to realize the greatest common divisor
- [luogu_p4556] [Vani has an appointment] tail in rainy days / [template] segment tree merging
- Alibaba's latest equity exposure: Softbank holds 23.9% and caichongxin holds 1.4%
- SQL tutorial: introduction to SQL aggregate functions
- Blocking queue
- Converter registration of easyexcel
- 正掩码、反掩码、通配符
- What are the benefits of taking NPDP
猜你喜欢

How to make computers have public IP

Chapter3 data analysis of the U.S. general election gold offering project

Sword finger offer II 041. Average value of sliding window

592. Fraction addition and subtraction
![[training day3] section [greed] [two points]](/img/4f/4130a1ade0ac0003adeddca780ff14.png)
[training day3] section [greed] [two points]

Leetcode · daily question · 592. fraction addition and subtraction · simulation

WPF visifire.charts4.6.1 tutorial with source code
![[luogu_p4820] [national training team] stack [mathematics] [physics] [harmonic progression]](/img/79/67399e6ce1908e38dc000e4b13a7ea.png)
[luogu_p4820] [national training team] stack [mathematics] [physics] [harmonic progression]

基于C语言的LR1编译器设计

平板模切机
随机推荐
Wechat campus laundry applet graduation design finished product (7) Interim inspection report
纯c手写线程池
How to make computers have public IP
We should learn to check the documented instructions of technical details
Jing Xiandong and other senior executives of ant group no longer serve as Alibaba partners to ensure independent decision-making
[training day4] card game [greed]
JWT login expiration - automatic refresh token scheme introduction
10 practical uses of NFT
Weice biological IPO meeting: annual revenue of 1.26 billion Ruihong investment and Yaohe medicine are shareholders
深度置信网络(DBN)【经典的DBN网络结构是由若干层 RBM(受限波尔兹曼机)和一层 BP 组成的一种深层神经网络】
Leetcode · daily question · 592. fraction addition and subtraction · simulation
Experience sharing of system architecture designers preparing for the exam: a tough battle for nearly three months
平板模切机
Wechat campus laundry applet graduation design finished product (4) opening report
The universe has no end. Can utonmos shine the meta universe into reality?
基于招聘广告的岗位人才需求分析框架构建与实证研究
CARLA 笔记(04)— Client 和 World (创建 Client、连接 World 、批处理对象、设置 Weather、设置 Lights、World snapshots)
Sword finger offer II 041. Average value of sliding window
基于RoBERTa-wwm动态融合模型的中文电子病历命名实体识别
One of yolox improvements: add CBAM, Se, ECA attention mechanism