当前位置:网站首页>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;
}
};
边栏推荐
- Good architecture is evolved, not designed
- RSS tutorial: aggregate your own information collection channels, rshub, freshrss, NetNewsWire
- Vscode -- create template file
- NoSQL -- three theoretical cornerstones of NoSQL -- cap -- Base -- final consistency
- Download address of each version of libtorch
- Jing Xiandong and other senior executives of ant group no longer serve as Alibaba partners to ensure independent decision-making
- Accuracy improvement method: efficient visual transformer framework of adaptive tokens (open source)
- 【idea】设置提取serialVersionUID
- WPF visifire.charts4.6.1 tutorial with source code
- Cognition -- classic of the road to success of hardware engineers
猜你喜欢

GoPro接入 - 根据GoPro官方文档/Demo,实现对GoPro的控制和预览

What are the benefits of taking NPDP

VSCode -- 创建模板文件

How to make computers have public IP

Realize the basic operations such as the establishment, insertion, deletion and search of linear tables based on C language

A Keypoint-based Global Association Network for Lane Detection

知识关联视角下金融证券知识图谱构建与相关股票发现

592. Fraction addition and subtraction

面试八股文之·TCP协议

Cognition -- classic of the road to success of hardware engineers
随机推荐
NFT 的 10 种实际用途
This points to problems, closures, and recursion
Travel notes from July 11 to August 1, 2022
Pure C handwriting thread pool
[daily question] 1206. Design jump table
Keras deep learning practice - recommend system data coding
C#测量工具示意图
10 practical uses of NFT
Excellent basic methods of URL parsing using C language
Various ways to use new
Sword finger offer II 041. Average value of sliding window
Jing Xiandong and other senior executives of ant group no longer serve as Alibaba partners to ensure independent decision-making
基于招聘广告的岗位人才需求分析框架构建与实证研究
现在还来得及参加9月份的PMP考试吗?
Windows10 installing SQL Server 2019
SQL tutorial: introduction to SQL aggregate functions
The salary level of programmers in various countries is a little miserable
[training day4] card game [greed]
Wechat campus laundry applet graduation design finished product (6) opening defense ppt
[training day3] section [greed] [two points]