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