代码拉取完成,页面将自动刷新
/* LTE */
class Solution {
public:
int cmp(const string &a, const string &b) {
int diff = 0;
for (int i = 0; i < a.size(); i++)
if (a[i] != b[i])
diff++;
return diff;
}
int recursion(const string &beginWord, const string &endWord,
const vector<string> &wordList, vector<bool> available) {
if (beginWord == endWord)
return 0;
// if (accumulate(available.begin(), available.begin(), 0) == 0)
// return -1;
int total = INT_MAX;
for (int i = 0; i < available.size(); i++) {
if (available[i] && cmp(beginWord, wordList[i]) == 1) {
available[i] = false;
int oneTry = recursion(wordList[i], endWord, wordList, available);
if (oneTry != -1) {
cout << beginWord << " -> " << endWord << endl;
total = total < oneTry ? total : oneTry;
}
available[i] = true;
}
}
if (total == INT_MAX)
return -1;
return total + 1;
}
int ladderLength(string beginWord,
string endWord, vector<string>& wordList) {
vector<bool> available(wordList.size(), true);
int res = recursion(beginWord, endWord, wordList, available) + 1;
return res != -1 ? res : 0;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。