《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’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- 经典的hash函数
unsigned int SDBMHash(char *str){ unsigned int hash = 0; while (*str) { // equivale ...
- LayoutParams布局
AbsoluteLayout.LayoutParams可以重新设置坐标,然后调用setLayoutParamsLinearLayout.LayoutParams可以调用setMargins();来移动 ...
- openstack kilo 命令行
把下面内容放到.bashrc中,或者直接执行也行. export OS_USERNAME=adminexport OS_PASSWORD=admin #根据实际密码来设 ...
- 居中未知元素(翻译https://css-tricks.com/centering-in-the-unknown/)
在web开发中,当你遇到居中元素时,知道越多关于元素本身和父级元素的信息,居中做起来就很轻松.但是,当遇到你一点都不知道的元素该怎么办? It's still kinda doable. 不会很难:已 ...
- HTML5 input date 移动端 IOS 不支持问题
1.placeholder 问题解决方法 对 input type date 使用 placeholder 的目的是为了让用户更准确的输入日期格式,iOS 上会有 date 不会显示 placehol ...
- C# 复合赋值操作符
前面讲过如何使用算术操作符来创建新值.例如,以下语句使用操作符+来创建比变量answer大42的一个值,新值将写入控制台: Console.WriteLine(answer + 42); 前面还讲过如 ...
- malloc动态分配字符串数组“ 一个月内的提醒”
//输出一个月提醒 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_R ...
- es6-promise.auto.js
使用sweetalert2的IE浏览器报错,导入文件 链接:https://pan.baidu.com/s/1mOcsN_o8m-7I7Rej1NPkiw 提取码:9xsj
- 开发必看 | iOS开发常用设计模式!
ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~(一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则: ...
- Linux下文件的压缩与解压缩
一.zip格式 zip可能是目前使用的最多的文档压缩格式.它最大的优点就是在不同的操作系统平台上使用.缺点就是支持 的压缩率不是很高,而tar.gz和tar.bz2在压缩率方面做得非常好. 我们可以使 ...