Leetcode 127
Posted 村雨sup
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 127相关的知识,希望对你有一定的参考价值。
class Solution { public: int ladderLength(string beginWord, string endWord, vector<string>& wordList) { vector<int> vis(wordList.size(),0); int res = INT_MAX; DFS(beginWord,endWord,wordList,res,vis); if(res == INT_MAX) return 0; else return res+1; } bool comp(string s1,string s2){ int cnt = 0; for(int i=0;i < s1.size();i++){ if(s1[i] != s2[i]) cnt++; } if(cnt == 1) return true; else return false; } void DFS(string beginWord, string endWord, vector<string> wordList,int& res,vector<int> vis){ if(beginWord == endWord){ int cnt = 0; for(int i=0;i < vis.size();i++){ if(vis[i] == 1)cnt++; } res = min(res,cnt); } for(int i=0;i < wordList.size();i++){ if(!vis[i] && comp(beginWord,wordList[i])){ vis[i] = 1; DFS(wordList[i],endWord,wordList,res,vis); vis[i] = 0; } } } };
_DFS写炸了,研究一下BFS
以上是关于Leetcode 127的主要内容,如果未能解决你的问题,请参考以下文章
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段