leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder
这道题使用bfs来解决,每次将满足要求的变换单词加入队列中。
wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不在词典中就不能加入队列。
pathCnt用来记录遍历到的某一个词使用的次数,做一个单词变换生成一个新单词,都需要判断这个单词是否在pathCnt中,如果在,则说明之前已经达到过,这次不用再计算了,因为这次计算的path肯定比之前多。pathCnt相当于剪枝。
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set wordSet(wordList.begin(),wordList.end());
if(!wordSet.count(endWord))
return ;
unordered_map<string,int> pathCnt;
queue<string> q;
q.push(beginWord);
pathCnt[beginWord] = ;
while(!q.empty()){
string word = q.front();
q.pop();
for(int i = ;i < word.size();i++){
string newWord = word;
for(char j = 'a';j <= 'z';j++){
newWord[i] = j;
if(newWord == endWord)
return pathCnt[word] + ;
if(wordSet.count(newWord) && !pathCnt.count(newWord)){
q.push(newWord);
pathCnt[newWord] = pathCnt[word] + ;
}
}
}
}
return ;
}
};
126. Word Ladder II
https://www.cnblogs.com/grandyang/p/4548184.html
class Solution {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
vector<vector<string>> res;
unordered_set<string> dict(wordList.begin(), wordList.end());
vector<string> p{beginWord};
queue<vector<string>> paths;
paths.push(p);
int level = , minLevel = INT_MAX;
unordered_set<string> words;
while (!paths.empty()) {
auto t = paths.front(); paths.pop();
if (t.size() > level) {
for (string w : words) dict.erase(w);
words.clear();
level = t.size();
if (level > minLevel) break;
}
string last = t.back();
for (int i = ; i < last.size(); ++i) {
string newLast = last;
for (char ch = 'a'; ch <= 'z'; ++ch) {
newLast[i] = ch;
if (!dict.count(newLast)) continue;
words.insert(newLast);
vector<string> nextPath = t;
nextPath.push_back(newLast);
if (newLast == endWord) {
res.push_back(nextPath);
minLevel = level;
} else paths.push(nextPath);
}
}
}
return res;
}
};
leetcode 127. Word Ladder、126. Word Ladder II的更多相关文章
- python如何转换word格式、读取word内容、转成html
# python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- leetcode 280.Wiggle Sort 、324. Wiggle Sort II
Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...
随机推荐
- django实现发送邮件功能
django实现邮件发送功能 1)首先注册一个邮箱,这里以163邮箱为例 2)注册之后登录,进行如下修改 找到设置,设置一个授权码,授权码的目的仅仅是让你有权限发邮件,但是不能登录到邮箱进行修改,发送 ...
- springboot+支付宝完成秒杀项目的初体验
springboot+支付宝完成秒杀项目的初体验 思考的问题: 首先是秒杀的商品查询,考虑到是热点数据,所以写一个接口读取当日批次的秒杀商品到redis中(那么接下来对商品的操作都放入redis中). ...
- React 零碎笔记
1.对数组的操作(添加.更新.删除) const posts = [...this.state.posts]; posts.push(post); this.setState({posts}); =& ...
- 05-Docker私有仓库
一.介绍私有仓库顾名思义,如果我们不想把docker镜像公开放到公有仓库中,只想在部门或团队内部共享docker镜像,这时私有仓库就来了. 二.私有仓库搭建与配置1.拉取私有仓库镜像,这里说明一下,私 ...
- numpy 参考:https://mp.weixin.qq.com/s?__biz=MzU1MjYzNjQwOQ==&mid=2247486010&idx=1&sn=e42e6706e0e285ecbfdbbd76fb4ff352&chksm=fbfe50accc89d9ba56a3167c519638f1327a5c5bf12ed59dd8c6de9b2c25baeec1f1f8ad5fb7&
a=np.array([,,,]) b=np.arange() print(a,b) [ ] [ ] 对应相乘 c2=a*b [ 0 2 6 12] 对应相乘再求和 c3=a.dot( ...
- 题解 [51nod1753] 相似子串
题解 [51nod1753] 相似子串 题面 解析 先考虑相等的时候怎么办, 我们考虑求出每个字母的贡献,这样字母相等的问题就可以用并查集来解决. 具体来说,我们先对于每个字母,把S中等于它的标为1, ...
- 三十四.MySQL主从同步 、主从同步模式
mysql51:192.168.4.51 主 mysql52:192.168.4.52 从 mysql50:192.168.4.50 客户机 1.MySQL一主一从 1.1 51,52 安装m ...
- Linux操作系统常用命令合集——第五篇-磁盘和文件系统操作(15个命令)
1.fdisk [命令作用] 用于观察硬盘实体使用情况.也可以对硬盘分区. 对于一块硬盘来讲,最多只能管理15个分区 [命令语法] fidisk [选项] [参数] [常用选项] -b& ...
- Vue.js 中的 v-cloak 指令
可以使用 v-cloak 指令设置样式,这些样式会在 Vue 实例编译结束时,从绑定的 HTML 元素上被移除. 当网络较慢,网页还在加载 Vue.js ,而导致 Vue 来不及渲染,这时页面就会显示 ...
- luogu 2934
同 bzoj3694 需要先求出最短路树 #include <iostream> #include <cstdio> #include <algorithm> #i ...