leetcode A String Replacement Problem---流程图
Replace all occurrence of the given pattern to ‘X’.
For example, given that the pattern=”abc”, replace “abcdeffdfegabcabc” with “XdeffdfegX”. Note that multiple occurrences of abc’s that are contiguous will be replaced with only one ‘X’.
First, it is not clear whether the problem mentions an in-place replacement or not, so be sure to ask this question during an interview. Many interview questions asked are purposely ambiguous. It is expected that the candidate ask thought-provoking questions of the interviewer in order to better answer the question. Here, we will assume that it is an in-place replacement.
思路:
1、還是用一前一后的兩個(gè)指針,前一個(gè)指針用于遍歷,后一個(gè)指針用于修改值。
2、如果pFast當(dāng)前所指的位置可以匹配,pFast向前移動(dòng)Pattern長度,并且記下匹配的信息,直到找到第一個(gè)不能匹配的點(diǎn)。
3、如果標(biāo)記顯示有子串匹配。則將pSlow替換為指定的字符。
4、如果pFast還沒有到Str的尾部,則將pFast賦給pSlow,因?yàn)閜Fast是剛剛找到的第一個(gè)未匹配的點(diǎn)。
?流程圖
測試代碼
#include<stdio.h> #include<assert.h> #include<string.h> int main() { const int MAX_N = 50; char Pattern[MAX_N]; char Str[MAX_N]; while(gets(Str) && gets(Pattern)) { StrReplace(Str, Pattern, 'X'); puts(Str); } return 1;
總結(jié)
以上是生活随笔為你收集整理的leetcode A String Replacement Problem---流程图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 详解RMQ LCA
- 下一篇: C++ 测量程序运行时间 任务管理看内存