leetcode BFS
1. word ladder
class Solution
{
public:
int ladderLength(string beginWord, string endWord, unordered_set<string> &wordDict)
{
queue<string> q;
q.push(beginWord);
unordered_map<string, int> umap;
umap[beginWord] = ;
for (q.push(beginWord); !q.empty(); q.pop())
{//这里用for的好处是这本身是一个模块化的过程:先入队;当队非空时进行循环,同时每个循环结束时都要把当前元素出队。用for不容易遗漏q.pop()。
string word = q.front();
int step = umap[word] + ;
for (int i = ; i<word.size(); i++)
{
for (char c = 'a'; c <= 'z'; c++)
{
if (word[i] != c)
{
char tmp = word[i];
word[i] = c;
if (word == endWord)//this line should here, not in the if statement below,coz endWord may not be in dict
return step;
if (wordDict.find(word) != wordDict.end() && umap.find(word) == umap.end())
{
umap[word] = step;
q.push(word);
}
word[i] = tmp;//don't forget to restore
}
}
}
}
return ;
}
};
leetcode BFS的更多相关文章
- [LeetCode] BFS解决的题目
一.130 Surrounded Regions(https://leetcode.com/problems/surrounded-regions/description/) 题目: 解法: 这道题 ...
- leetcode BFS解题思路
Word Ladder 思路一:单向bfs, 使用visited数组记录哪些已经访问过了, 访问过的就不允许再次入队, 同时这里想到的是使用26个英文字母,枚举可能的取值, 类似brute force ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- [LeetCode] 787. Cheapest Flights Within K Stops_Medium tag: Dynamic Programming, BFS, Heap
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...
- [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS
Given a string S and a character C, return an array of integers representing the shortest distance f ...
- [LeetCode] 675. Cut Off Trees for Golf Event_Hard tag: BFS
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...
随机推荐
- 快速创建SpringBoot+SSM解析
此处使用IDEA快速搭建SpringBoot应用,首先用SpringBoot搭建WEB工程: 然后点击Next生成项目,首次生成可能有点慢,下次创建的时候就会快很多,生成后的目录结构如下: 我们更改下 ...
- package-info类解读
类不能带有public.private访问权限.package-info.java再怎么特殊,也是一个类文件,也会被编译成package-info.class,但是在package-info.java ...
- 分段锁——ConcurrentHashMap
1.线程不安全的HashMap因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap. 2.效率低下的HashTable容 ...
- TCP网络参数优化
TCP连接的状态 TCP连接过程是状态的转换,促使状态发生转换的因素包括用户调用.特定数据包以及超时等,具体状态如下所示: CLOSED:初始状态,表示没有任何连接. LISTEN:Server端的某 ...
- C#判断字符串中是否包含一个子字符串是可以直接使用Contains()方法
1. 以前判断一个字符串中是否包含另一个子字符串时,习惯使用 IndexOf(); string str = "ABC@QQ"; if(str.IndexOf("@&qu ...
- 我们的GAME-TECH沙龙北京站完美收官了,都讨论了些啥?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由 腾讯游戏云 团队首发与云+社区 摘要:4月13日,腾讯游戏云GAME-TECH游戏开发者技术沙龙于北京举办.此次活动是腾讯游戏云游戏 ...
- 对.net事件的看法
对.net事件的看法 一.事件的本质 事件是软件系统里的两个子系统之间,或者两个模块之间,或者两个对象之间发送消息,并处理消息的过程.在面向对象的世界里,就可以统一认为是两个对象之间的行为. 两个对象 ...
- DataGrridView 当前行显示不同颜色
如果想让选中DataGridview的行显示不同颜色,就要通过DataGridview控件RowPerpaint事件中重新设置所选行的DefauleCellStyle属性来实现 private voi ...
- C# 利用log4net 把日志写入到数据库表中
效果图: 1:第一步创建SQL表结构 CREATE TABLE [dbo].[LogDetails] ( [LogID] int NOT NULL IDENTITY(1,1) , [LogDat ...
- android 使用图片轮播图---banner 使用
转自:https://github.com/youth5201314/banner 使用步骤 Step 1.依赖banner Gradle dependencies{ compile 'com.you ...