1. Word Ladder
    思路一:单向bfs, 使用visited数组记录哪些已经访问过了, 访问过的就不允许再次入队, 同时这里想到的是使用26个英文字母,枚举可能的取值, 类似brute force
    思路二:双向bfs,使用两个set,这里没有使用queue,是因为需要在queue里查询,不方便.
    另外,需要注意的一点是,每次遍历时,都是取size较小的来做搜索,初始时,各插入头和尾,之后每次取最小的set来拓展, 这样就实现了交替访问两个set,
    是两者的高度在 l/2, 这样可以缩短一般的时间

  2. 单词接龙 II
    思路一:因为要存储最终的结果,所以图是一定要建立的,通常我们可以通过维护每个节点的子节点,即一个map,每个map里是一个数组
    我们可以称之为children数组活着parent数组
    2)每遍历一层节点后,从wordlist中删除掉这些节点,因为本题是有源节点的,从源节点看,前一层的节点不应该再次呗遍历,因为长度变长了
    3)对于重复的问题,可以使用hastset来解决
    4)最后通过dfs,遍历输出结果

思路二:双向bfs + dfs
解法跟思路一类似,只是需要创建两个set,然后需要不断交换两个set

  1. Race Car
    思路一:bfs解法有一个重要的技巧,即利用一个set,记录已经出现过的路径,这样bfs枚举到重复路径时,可以直接跳过,这是一个有效的prunning method.

leetcode BFS解题思路的更多相关文章

  1. leetcode array解题思路

    Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...

  2. [LeetCode] 3Sum 解题思路

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  3. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  4. Z1. 广度优先搜索(BFS)解题思路

    /** BFS 解题思路 特点:从某些特定的节点开始,感染相邻的节点; 被感染的节点,再感染其相邻的节点,以此类推. 题目常见于数据结构包括 二维数组.树.图 **/ /** 1). 二维数组特定节点 ...

  5. [LeetCode] 45. Jump Game II 解题思路

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] 76. Minimum Window Substring 解题思路

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  7. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  8. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  9. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. hadoop之 hadoop用途方向

    hadoop是什么?Hadoop是一个开源的框架,可编写和运行分不是应用处理大规模数据,是专为离线和大规模数据分析而设计的,并不适合那种对几个记录随机读写的在线事务处理模式.Hadoop=HDFS(文 ...

  2. 军哥LNMP优化

    http://bbs.vpser.net/thread-8914-1-1.html http://www.zxsdw.com/index.php/archives/881/ 修改/usr/local/ ...

  3. Windows下生成自签名证书

    最近通过openssl生成了自签名的证书,总结成下面这张图. 说明:下载openssl0.9.8之后解压,然后运行bin\openssl.exe进入openssl运行环境,然后按上图中顺序执行命令.( ...

  4. RK3288 摄像头左右镜像

    系统:Android 5.1 设置摄像头左右镜像 diff --git a/frameworks/av/services/camera/libcameraservice/api1/CameraClie ...

  5. mysql命令之二:查看mysql版本的四种方法

    1:在终端下:mysql -V. 以下是代码片段: [shengting@login ~]$ mysql -V mysql Ver 14.7 Distrib 4.1.10a, for redhat-l ...

  6. 如何搭建struts2框架

    一.首先,下载5个Struts2核心jar包: commons-logging-1.1.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar struts2-core- ...

  7. Windows7下搭建Python2.7环境

    机器: Windows7_x86_64 步骤: 1.下载Python2.7 下载地址:https://www.python.org/downloads/ 2.安装Python2.7 双击安装包,安装过 ...

  8. kubernetes 学习 pod相关

    1  pod的状态: Pending, Running, Succeeded, Failed, Unknown 2  pod重启策略: Always(自动重启,是默认的) .  OnFailure(容 ...

  9. Jquery 页面初始化常用的三种方法以及Jquery 发送ajax 请求

    第一种 $(document).ready(function(){ //文档就绪事件 }); 第二种是第一种的简略写法,效果上和第一种是等效的. $(function(){ //文档加载事件,整个文档 ...

  10. mysql导出文本文件,加分隔符

    从mysql导出,再导入到oracle #!/bin/sh cd /u03/tools/machine_info rm -f data/machine_info.txt mysql -u用户名 -p密 ...