20200412

//我的代码
class Solution
{
public:
vector<string> stringMatching(vector<string> &words)
{
vector<string> res;
unordered_set<string> a;
for (int i = 0; i < words.size() - 1; i++)
{
for (int j = i + 1; j < words.size(); j++)
{
if (IsContainsStr(words[i], words[j]))
{
a.insert(words[j]);
}
if (IsContainsStr(words[j], words[i]))
{
a.insert(words[i]);
}
}
}
for (auto it : a)
{
res.push_back(it);
}
return res;
}
bool IsContainsStr(string str, string contains_str)
{
string::size_type idx = str.find(contains_str);
if (idx != string::npos)
{
return true;
}
else
{
return false;
}
}
};
//大佬的代码
class Solution {
public:
vector<string> stringMatching(vector<string>& a) {
vector<string> ans;
for(int i=0;i<a.size();++i) {
int ok = 0;
for(int j=0;j<a.size();++j) {
if( i != j) {
if(a[j].find(a[i]) != string::npos) ok = 1;
}
}
if(ok) ans.push_back(a[i]);
}
return ans;
}
};
总结:
npos可以表示string的结束位子
//改进
//是否为子列
bool IsContainsStr(string str, string contains_str)
{
return str.find(contains_str) != string::npos?true:false;
}

//我的代码
class Solution
{
public:
string entityParser(string text)
{
unordered_map<string, string> a;
StringReplace(text, "&", "&");
StringReplace(text, "'", "'");
StringReplace(text, """, "\"");
StringReplace(text, ">", ">");
StringReplace(text, "<", "<");
StringReplace(text, "⁄", "/");
return text;
}
//替换
void StringReplace(string &strBase, string strSrc, string strDes)
{
string::size_type pos = 0;
string::size_type srcLen = strSrc.size();
string::size_type desLen = strDes.size();
pos = strBase.find(strSrc, pos);
while ((pos != string::npos))
{
strBase.replace(pos, srcLen, strDes);
pos = strBase.find(strSrc, (pos + desLen));
}
}
};

class Solution {
public:
vector<int> processQueries(vector<int>& queries, int m) {
vector<int> res;
vector<int> a;
for(int j=0;j<m;j++)
{
a.push_back(j+1);
}
for(int i=0;i<queries.size();i++)
{
auto it=find(a.begin(),a.end(),queries[i]);
auto index=distance(begin(a),it);
res.push_back(index);
gengxin(a,queries[i],index);
}
return res;
}
void gengxin(vector<int> &a,int nums,int i)
{
a.erase(a.begin()+i);
a.insert(a.begin(),nums);
}
};
总结:
//找到元素 返回下表
auto it=find(a.begin(),a.end(),queries[i]);
auto index=distance(begin(a),it);