Re: [闲聊] 每日leetcode

楼主: oin1104 (是oin的说)   2024-10-06 13:31:10
题目:
可以从中间插入“单字”
不能直接接字 要有空格那种
看能不能把两个橘子变一样
思路:
把他变成一个字单一个单字的vector
然后再two pointer
我要打手枪
```cpp
class Solution {
public:
bool areSentencesSimilar(string sentence1, string sentence2)
{
sentence1 += " ";
sentence2 += " ";
int n1 = sentence1.size();
int n2 = sentence2.size();
vector<string> w1;
vector<string> w2;
int l = 0;
int r = 0;
string xd;
for(int i = 0 ; i < n1 ; i ++)
{
if(sentence1[i] == ' ')
{
w1.push_back(xd);
xd.clear();
}
else
{
xd.push_back(sentence1[i]);
}
}
for(int i = 0 ; i < n2 ; i ++)
{
if(sentence2[i] == ' ')
{
w2.push_back(xd);
xd.clear();
}
else
{
xd.push_back(sentence2[i]);
}
}
for( ; l < min(w1.size() ,w2.size() ); l ++)
{
if(w1[l] != w2[l]) break;
}
for( ; r < min(w1.size() ,w2.size() ); r ++)
{
if(w1[w1.size()-1-r] != w2[w2.size()-1-r]) break;
}

return (l+r >= min(w1.size() ,w2.size() ));
}
};```
作者: CanIndulgeMe (CIM)   2024-10-06 13:33:00
东大资工的荣耀你终于使用学网的IP了
楼主: oin1104 (是oin的说)   2024-10-06 13:34:00
真的欸 134.208
作者: JIWP (JIWP)   2024-10-06 13:34:00
我好佩服你
作者: sixB (6B)   2024-10-06 13:56:00
大学生真的好厉害

Links booklink

Contact Us: admin [ a t ] ucptt.com