1 Star 0 Fork 0

MetaverseMobile/leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
127_单词接龙_递归.cpp 1.70 KB
一键复制 编辑 原始数据 按行查看 历史
TieNan2019 提交于 2020-11-05 10:31 . Create 127_单词接龙_递归.cpp
/* 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;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/MetaverseMobile/leetcode.git
[email protected]:MetaverseMobile/leetcode.git
MetaverseMobile
leetcode
leetcode
main

搜索帮助