当前位置:网站首页>【LeetCode】541. Reverse string II
【LeetCode】541. Reverse string II
2022-06-24 08:58:00 【Uaena_ An】
541. Reverse string II

Reading questions
- The question is take K In the character , Turn it all over , But the starting position of the next reversal should be 2*K Starting from the position of .
- If the next starting position starts , Following character <K individual , Then turn it all over .
- If at first Characters are less than K, Just flip it all over .
🧸 Problem solving
I look at the
class Solution {
public:
string reverseStr(string s, int k) {
int len = s.size();// Record the next position of the last character
for(int i = 0;i<len;i+=2*k)
{
if(i+k<len)
{
reverse(s.begin()+i,s.begin()+i+k);
}
else{
reverse(s.begin()+i,s.begin()+len);
}
}
return s;
}
};
🧸 Decoding code
i Is the starting position of each flipped character .i+k Is the end position of each flip .len Is the next position of the last character for(int i = 0;i<len;i+=2*k)
forLoop start , Every timei+=2*k, Directly find the starting position of the next inverted character .
if(i+k<len)
iEvery time+=2*k, Then as long asiThe location of+k, Not more thanlen, Just reverseiTokCharacters .
else
iThe location of+k, More than thelen, It indicates that the character ratio that can be flipped K Small , Then turn it all over directly .
Tail control
for example
k = 2i + kTurn it over2Characters .
So the subscript of the end position is2, That is, the position of the third character , and reverse It just needs to be closed on the left and opened on the right , See the following explanation for details .
reverse
reverse(left,right)The parameter of the function is left closed and right open .
The official explanation is : Flipped characters : It means inleftTorightThe character of the interval , containleftThe character pointed to , It doesn't containrightThe character pointed to .
🧸 The first time I wrote code
The algorithm written for the first time : It's so long , The train of thought is to encounter a problem , Solve it once .
if(s.size()>=k)// Avoid flipping with only one character , direct return
{
int begin = 0, end = s.size();//begin Is to find 2k The location of ,end It's the end
int count = 0, prev = 0;//count Is the number of records flipped ,prev Yes k The position where the reversal begins
while (begin < end) //begin Keep going back , Until you're done
{
count = 0;//count Each start will be set 0, Is the number of characters to be flipped
prev = begin;// Reset to next k The position where the reversal begins
while (begin < end &&count < 2*k)// In the calculation Is it enough 2k
{
if (count != 2 * k)
{
++begin;
++count;
}
}
if ((count == 2 * k) || (count < 2 * k && count >= k))
{
reverse(s.begin() + prev, s.begin() + prev+k);// Flip prev To k Characters between
}
else if (count < k && count!=1)
{
// Flip prev All characters to the end , because count Only record to end The location of
reverse(s.begin() + prev, s.begin() +prev+count);
}
}
}
else
{
if(s.size() != 1)// Here is If k Directly greater than The number of characters , Just flip it all
{
reverse(s.begin(),s.end());
}
}
return s;
}
This code is redundantly written , There seem to be a lot of useless variables , I haven't changed any more . This code can be passed .
come on. , I wish you get your favorite offer!
边栏推荐
- Leetcode -- wrong set
- Data middle office: middle office practice and summary
- Data middle office: a collection of middle office construction architectures of large domestic factories
- OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
- 520. detect capital letters
- China chip Unicorn Corporation
- Liunx change the port number of vsftpd
- Camera projection matrix calculation
- MySQL | view notes on Master Kong MySQL from introduction to advanced
- Pymysql inserts data into MySQL and reports an error for no reason
猜你喜欢

MySQL | 存储《康师傅MySQL从入门到高级》笔记

關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL

【PyTorch基础教程30】DSSM双塔模型代码解析

One article explains in detail | those things about growth

【LeetCode】415. 字符串相加
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

Spark - the number of leftouterjoin results is inconsistent with that of the left table

MySQL | store notes of Master Kong MySQL from introduction to advanced
![[pytoch basic tutorial 31] youtubednn model analysis](/img/18/dbeab69894583f6e5230772ce44652.png)
[pytoch basic tutorial 31] youtubednn model analysis

玄铁E906移植----番外0:玄铁C906仿真环境搭建
随机推荐
Analyze the meaning of Internet advertising terms CPM, CPC, CPA, CPS, CPL and CPR
pm2 部署 nuxt3.js 项目
双指针模拟
第七章 操作位和位串(三)
What is graph neural network? Figure what is the use of neural networks?
OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
GradScaler MaxClipGradScaler
Why can ping fail while traceroute can
Using skills of xargs -- the way to build a dream
leetcode——错误的集合
随笔-反思
等保备案是什么意思?应该去哪里办理备案?
Solving linear equations with MATLAB ax=b
110. balanced binary tree recursive method
Earthly container image construction tool -- the road to dream
What is the future development trend of Business Intelligence BI
The form image uploaded in chorme cannot view the binary image information of the request body
Floating error waiting for changelog lock
Database migration from PostgreSQL to MySQL
基于单片机开发的酒精浓度测试仪方案
