《Cracking the Coding Interview》——第18章:难题——题目10
2014-04-29 04:22
题目:给定一堆长度都相等的单词,和起点、终点两个单词,请从这堆单词中寻找一条变换路径,把起点词变成终点词,要求每次变换只能改一个字母。
解法:Leetcode中有Word Ladder,这题基本思路一致。
代码:
// 18.10 Given a list of words, all of same length. Given a source and a destionation words, you have to check if there exists a path between the two. Every time you may change only one letter in the word.
// This is my code from leetcode problem set: word ladder
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std; class Solution {
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
unordered_map<string, vector<string> > back_trace;
vector<unordered_set<string> > level(); dict.insert(start);
dict.insert(end); int flag, nflag;
flag = ;
nflag = !flag;
level[flag].insert(start); unordered_set<string>::iterator usit;
char ch, old_ch;
string word;
while (true) {
flag = !flag;
nflag = !nflag;
level[flag].clear();
for (usit = level[nflag].begin(); usit != level[nflag].end(); ++usit) {
dict.erase(*usit);
}
for (usit = level[nflag].begin(); usit != level[nflag].end(); ++usit) {
word = *usit;
for (size_t i = ; i < word.size(); ++i) {
old_ch = word[i];
for (ch = 'a'; ch <= 'z'; ++ch) {
if (ch == old_ch) {
continue;
}
word[i] = ch;
if (dict.find(word) != dict.end()) {
back_trace[word].push_back(*usit);
level[flag].insert(word);
}
}
word[i] = old_ch;
}
}
if (level[flag].empty() || level[flag].count(end) > ) {
// found or not found
break;
}
} single_result.clear();
for (size_t i = ; i < result.size(); ++i) {
result[i].clear();
}
result.clear(); if (!back_trace.empty()) {
recorverPath(back_trace, end);
} return result;
}
private:
vector<vector<string> > result;
vector<string> single_result; void recorverPath(unordered_map<string, vector<string> > &back_trace, string cur) {
if (back_trace.count(cur) == ) {
// this word has no back trace, it is unreachable.
vector<string> single_path(single_result); single_path.push_back(cur);
reverse(single_path.begin(), single_path.end());
result.push_back(single_path);
return;
} const vector<string> &v = back_trace[cur];
vector<string>::const_iterator usit; single_result.push_back(cur);
for (usit = v.begin(); usit != v.end(); ++usit) {
recorverPath(back_trace, *usit);
}
single_result.pop_back();
}
};
《Cracking the Coding Interview》——第18章:难题——题目10的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目12
2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- Linux远程桌面(二)
上一篇远程桌面采用的独立服务配置不适用于过多用户,这一篇采用超级Internet服务器搭建vnc服务可以解决多用户问题. vnc之xinetd服务搭建配置 Linux远程桌面(一):vnc之独立服务 ...
- 【转】Android Support 包里究竟有什么
随着 Android 5.0 Lollipop 的发布,Android 又为我们提供了更多的支持包,但是我相信大部分开发者都同我之前一样不知道这些包里究竟有些什么东西,我们应该在什么时候使用它.现在, ...
- Spring boot 集成三种定时任务方式
三种定时任务方式分别为 org.springframework.scheduling.annotation.Scheduled java.util.concurrent.ScheduledExecut ...
- Network in Network 笔记
传统CNN里的卷积核是一个generalized linear model(GLM)之后经过一个sigmoid(现在通常是ReLu)的非线性激励函数,假设卷积有K个filter,那么这K个filter ...
- Apache 负载均衡 端口转发 配置
转载自:https://blog.csdn.net/snihcel/article/details/38844323 [端口转发配置] 通过http_proxy做tomcat的端口转发: ...
- securecrt颜色设置
https://blog.csdn.net/zq710727244/article/details/53909801
- 11_1_GUI
11_1_GUI 1. AWT AWT(Abstract Window Toolkit)包括了很多类和接口,用于Java Application的GUI(Graphics User Interface ...
- vue项目中的函数封装
项目中一般都会有fun.js这类的文件,里面有各种的如转换时间格式的,处理转换的等等函数方法. 其实经常用到的去获取基本数据的接口也可以封装成一个方法,方便复用. 如上面所标,获取列表数据之前需要先获 ...
- v-show
v-show的原理是当值为false的时候,元素display:none 隐藏了元素且脱离文档流,但是在dom 中仍然存在. 与v-if使用场景不同,文档中提到,当需要高频切换的时候使用v-show ...
- ABAP 调用远程rfc
ABAP 调用rfc DESTINATION附加项后面接的是远程目标名称,该目标在事务SM59中设定,其中包含连接和登录远程系统所需的全部参数信息.如果调用的就是本机的RFC目标,则DESTINATI ...