本文共 1514 字,大约阅读时间需要 5 分钟。
#define _for(i,a,b) for(int i = (a); i < (b); i++)class Solution {public: vectorspellchecker(vector wordlist, vector queries) { vector rnt; set ws; map wxs; map wys; _for(i, 0, wordlist.size()) ws.insert(wordlist[i]); _for(i, 0, wordlist.size()) { string tmp = wordlist[i]; _for(j, 0, tmp.size()) tmp[j] = tolower(tmp[j]); if (!wxs.count(tmp)) wxs.insert({tmp, i}); } _for(i, 0, wordlist.size()) { string tmp = wordlist[i]; _for(j, 0, tmp.size()) { if (tmp[j] == 'a' || tmp[j] == 'e' || tmp[j] == 'i' || tmp[j] == 'o' || tmp[j] == 'u') tmp[j] = '*'; } if (!wys.count(tmp)) wys.insert({tmp, i}); } _for(i, 0, queries.size()) { if (ws.count(queries[i])) { rnt.push_back(queries[i]); continue; } string tmp = queries[i]; _for(j, 0, tmp.size()) tmp[j] = tolower(tmp[j]); auto pp = wxs.find(tmp); if (pp != wxs.end()) { rnt.push_back(wordlist[pp->second]); continue; } _for(j, 0, tmp.size()) if (tmp[j] == 'a' || tmp[j] == 'e' || tmp[j] == 'i' || tmp[j] == 'o' || tmp[j] == 'u') tmp[j] = '*'; auto pp2 = wys.find(tmp); if (pp2 != wys.end()) { rnt.push_back(wordlist[pp2->second]); continue; } rnt.push_back(""); } return rnt; }};
这个优化后的版本保持了代码的原有功能,但更加简洁和易读。主要进行了以下改写:
这个版本在保持功能完整性的同时,更加符合技术写作的习惯,阅读体验也得到了显著提升。
转载地址:http://mlgyk.baihongyu.com/