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的更多相关文章

  1. python如何转换word格式、读取word内容、转成html

    # python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...

  2. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  3. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  4. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  5. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  6. 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 ...

  7. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  8. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  9. leetcode 280.Wiggle Sort 、324. Wiggle Sort II

    Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...

随机推荐

  1. TLS1.3对CIP的影响(对密码套件的解释)

    1.术语定义的即使(算法)Definition of terms (optional) Cipher Suite  :通信数据保护规范,对TLS指定对端身份验证,关键技术机制,后续数据加密和数据验证机 ...

  2. 【转载】抓包工具tcpdump用法说明

    转载地址:http://www.cnblogs.com/f-ck-need-u/p/7064286.html tcpdump采用命令行方式对接口的数据包进行筛选抓取,其丰富特性表现在灵活的表达式上. ...

  3. CentOS8 NextCloud 私有云存储搭建

    本文首发:https://www.somata.work/2019/CentOS8NextCloudBuild.html 之前发现 Owncloud 越来越捞了,推出了企业版和社区版,近几日突然发现原 ...

  4. AD19新功能之交互式等长

    多信号线等长 选中需要等长的信号线: 选择“Interaction Length Tuning”命令,然后在网络线上点击一下,然后 tab 键暂停: 在Properties面板中,修改Source部分 ...

  5. P1168 中位数[堆 优先队列]

    题目描述 给出一个长度为NNN的非负整数序列AiA_iAi​,对于所有1≤k≤(N+1)/21 ≤ k ≤ (N + 1) / 21≤k≤(N+1)/2,输出A1,A3,…,A2k−1A_1, A_3 ...

  6. subprocess模块笔记

    subprocess笔记 import subprocess subprocess.call("netstat -ano") #父进程等待子进程完成任务,返回执行结果和结束信息0或 ...

  7. 后端token认证模板

    1.创建一个视图 from rest_framework import exceptions from app01 import models from rest_framework.authenti ...

  8. 48、[源码]-Spring容器创建-初始化事件派发器、监听器等

    48.[源码]-Spring容器创建-初始化事件派发器.监听器等 8.initApplicationEventMulticaster();初始化事件派发器: 获取BeanFactory 从BeanFa ...

  9. 【编程语言】Kotlin之扩展函数

    简介: 平时Android开发中会使用各种各样的工具类函数,大部分工具类都是对原有对象的一种扩展,例如: public static void startActivity(Activity act, ...

  10. PHP读取文件内容的方法

    下面我们就为大家详细介绍PHP读取文件内容的两种方法. 第一种方法:fread函数 <?php $file=fopen('1.txt','rb+'); echo fread($file,file ...