当前位置:网站首页>2000. reverse word prefix
2000. reverse word prefix
2022-06-12 13:51:00 【Doll noodles I】
2000. Reverse word prefix
I'll give you a subscript from 0 Starting string
wordAnd a characterch. findchFirst occurrence of subscripti, reversewordFrom the subscript0Start 、 Until the subscriptiend ( Including subscripti) That part of the character . IfwordThere are no characters inch, Then no operation is required .for example , If
word = "abcdefd"Andch = "d", Then you should reverse From the subscript 0 Start 、 Until the subscript3end ( Including subscript3). The result string will be"dcbaefd".return Result string .
Example 1:
Input :word = "abcdefd", ch = "d"
Output :"dcbaefd"
explain :"d" The first time it appears in the subscript 3 .
Reverse subscript 0 To the subscript 3( Including subscript 3) This character , The result string is "dcbaefd" .
Example 2:
Input :word = "xyxzxe", ch = "z"
Output :"zxyxxe"
explain :"z" The first and only occurrence is in the subscript 3 .
Reverse subscript 0 To the subscript 3( Including subscript 3) This character , The result string is "zxyxxe" .
Example 3:
Input :word = "abcd", ch = "z"
Output :"abcd"
explain :"z" There is no in word in .
No inversion is required , The result string is "abcd" .
Their thinking :
- Find the position of the first character in the string
- Determine whether the character can be found
- If the character is not found , Just return the original string directly
- If the character is found , After the character is reversed from the beginning to the position found , Return string
Code :
// Because it's easier , So just use algorithm
class Solution
{
public:
string reversePrefix(string word, char ch)
{
// Find the subscript of the first matching character in the string , If you can't find it, go back -1
auto it = word.find_first_of(ch);
// Reverse the character from the start position to the current position . Because the return cannot be found -1, -1 + 1 = 0; So there's no need to judge
reverse(word.begin(), word.begin() + it + 1);
// Just return the string ;
return word;
}
};
边栏推荐
猜你喜欢
颜色编码格式介绍

Data type conversion and conditional control statements

Web3.0, the era of "stimulating creativity"
![[WUSTCTF2020]颜值成绩查询-1](/img/90/e4c2882357e0a1c6a80f778887e3f5.png)
[WUSTCTF2020]颜值成绩查询-1

Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform

Record some settings for visual studio 2019

【mysql进阶】索引分类及索引优化方案(五)
![[wustctf2020] selfie score query -1](/img/dc/47626011333a0e853be87e492d8528.png)
[wustctf2020] selfie score query -1

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging

Briefly describe the difference between CGI and fastcgi
随机推荐
聊聊MySQL的10大经典错误
Language skills used in development
Binary tree traversal
Codeforces 1638 A. reverse - simple thinking
Codeforces 1638 D. Big Brush —— BFS
Greed issues - Egypt scores
Pytorch framework
FFmpeg 学习指南
Talk about the top 10 classic MySQL errors
Use of awlive structures
Web3.0,「激发创造」的时代
List of common ACM knowledge points (to be continued)
Informatics Olympiad all in one 1000: introductory test questions
xcode 调试openGLES
Convert the string to hexadecimal string and display it
Recursion of subviews of view
Codeforces 1629 F2. Game on sum (hard version) - Yang Hui's triangle, violence, finding rules
Simple implementation of gpuimage chain texture
Paw advanced user guide
Cmake basic tutorial - 01 a-hello-cmake