leetcode 126 Word Ladder II

题目连接

https://leetcode.com/problems/word-ladder-ii/  

Word Ladder II

Description

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

 

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

跟127那道题差不多,只不过多了要保存路径(所有解)。
一开始用dfs结果超时,一看要求最短路径马上就想到了bfs。
于是胡乱一敲提交上去tle QAQ。
bfs太慢好吧那么来个双向的吧,不幸的是经过一些奇怪的姿势,只能水到31组数据
然后一直wa(总是少了几组解),越写越烦懒得改了。网上搜题解才搞明白了。
具体做法是: 分层bfs,每遍历完一层再扩展下一层。在扩展过程中记得用map
保存新节点和旧节点的父子关系,同时在dic中删除已出现的word防止重复。
最后利用得到的这个map从end出发找start。好了终于过了 卒...
talk is cheap show me the code

class Solution {
    using ves = vector<string>;
    using vees = vector<ves>;
    using Hash = map<string, vector<string>>;
public:
    vees findLadders(string start, string end, unordered_set<string> &dic) {
        if(!bfs(start, end, dic)) return ans;
        ves rec;
        dfs(start, end, rec);
        return ans;
    }
private:
    void dfs(string &start, string &end, ves &rec) {
        rec.push_back(end);
        if(start == end) {
            ves temp(rec);
            reverse(temp.begin(), temp.end());
            ans.push_back(temp);
            rec.pop_back();
            return;
        }
        for(auto &r: hsh[end]) {
            dfs(start, r, rec);
        }
        rec.pop_back();
    }
    bool bfs(string start, string end, unordered_set<string> &dic) {
        int cur = 0;
        unordered_set<string> Q[2];
        Q[cur].insert(start);
        auto expandState = [&](unordered_set<string> &from, unordered_set<string> &to) {
            to.clear();
            for(auto &r: from) {
                for(int i = 0; i < (int)r.size(); i++) {
                    string temp = r;
                    for(char j = 'a'; j <= 'z'; j++) {
                        char ch = temp[i];
                        if(ch == j) continue;
                        temp[i] = j;
                        if(dic.find(temp) != dic.end()) {
                            to.insert(temp);
                            hsh[temp].push_back(r);
                        }
                        temp[i] = ch;
                    }
                }
            }
        };
        while(!Q[cur].empty()) {
            for(auto &r: Q[cur]) dic.erase(r);
            expandState(Q[cur], Q[cur ^ 1]);
            cur ^= 1;
            if(Q[cur].find(end) != Q[cur].end()) return true;
        }
        return false;
    }
private:
    Hash hsh;
    vees ans;
};

转载于:https://www.cnblogs.com/GadyPu/p/5631344.html

Leetcode 126. Word Ladder II Python BFS 实现 题目:126.Word Ladder II https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWordandendWord), and a dictionary's word list, find all shortest transformation sequence(s) frombeginWor... 阅读详情

相关推荐

量化平台深度测评:聚宽 / 米筐 / 迅投 QMT 对比分析

本文对比测评国内主流量化交易平台聚宽、米筐和迅投QMT,从回测性能、实盘稳定性和高级功能三方面展开分析。测试显示:聚宽在低频策略回测中效率最高(128秒),米筐次之;迅投QMT凭借C++直连架构实现最低延迟(150ms)和最高订单撤销成功率(98.5%)。平台特色方面,QMT的智能拆单算法可减少25%滑点损失,聚宽的因子库支持版本控制和缓存复用。建议低频策略选聚宽,高频交易选QMT,团队协作优先考虑聚宽。测试数据均来自合规实盘环境,建议根据策略特性进行试用验证。

tianchi2009123的博客 7365

leetcode 126. Word Ladder II

今天这篇笔记讲讲一直都弄得不是很清楚的回溯算法。回溯写法用于dfs比较常见,由于深度优先,如果搜完之后还需要继续搜索,那么就只好往前回溯了。深度优先之所以需要这样遍历,是为了不重复搜索,特别是这种树形结构的。在形式上比较像bfs,但实质上是dfs,因为一定是在一个搜索路径下走完,才来寻找其他的路径。 这一题下面这种解法确实有一些难度,先是用bfs来建立树,并且利用层次进行标明,然后再利用dfs反

独钓寒江雪 440

Vivado ILA调试全攻略:从HDL实例化到Block Design的5个实战技巧

本文全面解析了Vivado集成逻辑分析仪(ILA)的五种核心调试方法,涵盖从HDL代码实例化到Block Design集成的实战技巧。详细对比了不同方法的优缺点与适用场景,并重点介绍了如何利用Debug Hub高效管理多时钟域复杂调试,旨在帮助FPGA开发者精准定位问题,显著提升板级调试效率。

weixin_29214199的博客 376

leetcode 126. Word Ladder II

leetcode 126. Word Ladder II   写这一题 真的非常适合复习bfs与dfs啊 import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.ut

独钓寒江雪 559

工业缺陷检测深度学习方法综述

异常:点异常、 上下文异常和集群异常。点异常:又称为离群值 (outliers)[9], 描述数值上偏离正 常样本的独立数据。与异常相近的概念还包括新颖点(novelty)和分布外数据。上下文异常:同样描述数据点,其数值属于正常范围,但不符合局部上下文规律。集群异常:描述一系列相关数据的集合,集合中的每一个实例的数值在单独考察时都处于正常值域,但集 合整体的相关性特征不服从正常模式。一般的图像异常检测往往仅需区分正常与异常样本,而工业缺陷检测更关注于检测 图像中的异常像素.

2301_80355452的博客 1842

GAMIT/GLOBK 10.71在Linux环境下的GNSS数据处理全流程解析(附实战案例)

本文详细解析了在Linux环境下部署和运行GAMIT/GLOBK 10.71进行GNSS数据处理的完整流程。从系统环境配置、软件编译安装、关键表格文件更新,到运行自带示例和实战案例,提供了清晰的步骤与避坑指南,帮助用户掌握这一高精度GNSS数据处理工具的核心使用方法。

kmeans3miner的博客 309

LeetCode: 126. Word Ladder II

LeetCode: 126. Word Ladder II 最近工作和毕设压力比较大, 刷 LeetCode 有点不在状态,代码可能有点难看。不过,整体的思路应该是没有问题的。 题目描述 Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transfor...

杨领well的专栏 676

竞拍算法(Auction Algorithm)原理及工作过程分析

因一些项目工作需要对竞拍算法进行深入了解。但百度了大部分资料都未找到一篇文章能对此算法进行较为深入的介绍。在一番努力之下,终于找打了最初提出该算法的论文,本文内容主要结合该论文对竞拍算法进行分析。 竞拍算法简介 竞拍算法(Auction Algorithm)是一种用于解决分配问题的分布式算法,经相关文献查询,最早由麻省理工学院信息和决策系统实验室的Bertsekas(教授?)提出 1,于1988年发表于期刊Annals of Operations Research。 顾名思义,竞拍算法本质就是模拟人类拍.

weixin_47546390的博客 3万+

LeetCode力扣之126. Word Ladder II

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a timeEach tr...

LeeWei 651

手把手教你学Simulink--使用Simulink进行双冗余电机驱动系统的故障切换验证

提高系统可靠性:当一个电机或其驱动器出现故障时,另一个可以立即接管,避免系统停机。增强安全性:对于一些关键任务的应用场景(如飞行控制),确保即使在硬件故障情况下也能维持基本操作能力。简化维护:允许在不影响系统正常工作的情况下进行维护和更换故障部件。确定可能发生的故障类型(如过流、欠压、温度过高),以及每种故障对应的条件。通过上述步骤,我们简要介绍了如何基于Simulink实现双冗余电机驱动系统的故障切换验证。

MHD0815的博客 437

STM32堆栈溢出问题及解决方法

堆栈是用于存储函数调用和局部变量的内存区域,它是一种先进后出的数据结构。如果函数嵌套层级过深或者局部变量占用的内存空间过大,堆栈的大小可能不足以容纳这些信息,从而导致堆栈溢出。在一些特殊情况下,可以使用堆内存替代堆栈,以避免堆栈溢出问题。在STM32的工程中,可以通过修改链接脚本文件(.ld文件)中的堆栈大小参数来增加堆栈空间。通常,堆栈大小设置为RAM的一小部分,但需要根据具体应用的需求进行调整。堆栈溢出是嵌入式系统开发中常见的问题之一,特别是在使用STM32微控制器时。3.3 使用堆内存替代堆栈。

ZwcCsharp的博客 2593

【难点+重点BFS】LeetCode 126. Word Ladder II

LeetCode 126. Word Ladder II

Allenlzcoder的博客 1778

VSS shielding线可以floating吗?

shielding vss不打孔,就相当于dummy metal,如果周围有其他干扰线,他们影响了这段dummy metal,这段dummy metal仍然会对目标线产生干扰,bug的是,工具知道这段dummy是vss,那么计算寄生的时候仍然把它当vss,就有可能噪声没有完全消除,而用户又看不见。而真正接地的线,相当于一直是0,就不会对目标线产生干扰,一般加shield是在route之前,这时候shield能尽量覆盖目标线,等到绕线后再加就会出现shield只覆盖到一部分目标线的情况。

m0_61544122的博客 6633

【leetcode】【hard】126. Word Ladder II​​​​​​​

126. Word Ladder II Given two words (beginWordandendWord), and a dictionary's word list, find all shortest transformation sequence(s) frombeginWordtoendWord, such that: Only one letter can be c...

lemonade13的博客 174

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode-126. Word Ladder II [C++][Java]

Given two words,beginWordandendWord, and a dictionarywordList, returnall theshortest transformation sequencesfrombeginWordtoendWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words[beginWord,

贫道绝缘子的博客 1131

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Leetcode 126 Word Ladder II(BFS)

题目连接:Leetcode 126 Word Ladder II解题思路:将每个字符串看做结点,结点间存在边的条件为两个字符串差异字符数等于1。建图后,从起始结点开始进行BFS,因为要求最短路径,所以用一个数组记录每个结点是否遍历过。class Solution { public: bool trans(string a, string b) { bool diff = false; ...

不慌不忙、不急不躁 871

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

[leetcode] 126. Word Ladder II 解题报告

题目链接: https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord toendWord, s

小榕流光的专栏 4053

LeetCode:126Word Ladder II

problem LeetCode 126 Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in th

erzhushashade的博客 227

leetcode 126 Word Ladder II

题目概述: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a ti...

qq_37720278的博客 233

【Leetcode】126. Word Ladder II

题目地址: https://leetcode.com/problems/word-ladder-ii/ 给定两个单词作为起点和终点,每一步允许从单词中改变某个位置的字母,变为另一个单词。再给定一个单词的列表,问从起点到终点的所有最短路径,要求路径中所有单词都要在列表里(起点不必在,终点必须在)。题目保证所有单词都由小写字母组成。 思路是BFS建图 + DFS。先从起点出发,用BFS的方式找到终点,并一路建图,建图的时候需要注意,BFS的同层的单词之间不要连边,每条边只在不同层的单词连起来。并且,一旦发现到了

数学、算法爱好者的博客 491

【LeetCode】解题126:Word Ladder II(BFS算法)

LeetCode解题 126:Word Ladder II(BFS算法)Problem 126: Word Ladder II [Hard]解题思路Solution (Java)修改过程 Problem 126: Word Ladder II [Hard] Given two words (beginWord and endWord), and a dictionary’s word list, ...

Fayedily的博客 774

LeetCode(126) Word Ladder II

题目如下: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word

BUILD 3015

Leetcode 126. Word Ladder II

126. Word Ladder II 题目 题目解析 基于广度优先的改进方法 126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from begin...

新安浅滩的博客 720

LeetCode 126. Word Ladder II(单词梯子)

原题网址:https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, s

James Pan 1024
上一篇: leetcode 367 Valid Perfect Square
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值