《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’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- April 26 2017 Week 17 Wednesday
We read the world wrong and say that it deceives us. 我们把世界看错了,反而说它欺骗了我们. It is not a cakewalk to see ...
- 22 Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- php之判断点在多边形内的api
1.判断点在多边形内的数学思想:以那个点为顶点,作任意单向射线,如果它与多边形交点个数为奇数个,那么那个点在多边形内,相关公式: <?php class AreaApi{ //$area是一个多 ...
- IOS 运行循环
. 运行循环========================================在iOS的应用程序中,应用程序启动之后,系统即会创建一个运行循环监听用户的交互. 以下代码其本质是在运行循环 ...
- Netbackup-Oracle数据库恢复演练(上)
目录 第一章NBU实施内容 1 第二章 环境准备 1 2.1添加LICENSE: 1 2.2安装配置NBU客户端(linux,unix相同) 2 2.3配置并验证oracle备份脚本 3 2.4添加备 ...
- 【转】jpg png区别和使用
为什么想整理这方面的类容,我觉得就像油画家要了解他的颜料和画布.雕塑家要了解他的石材一样,作为网页设计师也应该对图片格式的特性有一定了解,这样才能更好的表达你的创意和想法. 除此之外,我们在平时工作中 ...
- 剑指offer54 表示数值的字符串
错误的代码: class Solution { public: bool isNumeric(char* string) { if(string == NULL) return false; if(* ...
- HttpServletRequest request 获取form参数的两种方式
@RequestMapping(value="/pay",method = RequestMethod.POST) public String buildRequest(HttpS ...
- ajax(form)图片上传(spring)
第一步:spring-web.xml <!--配置上传下载--> <bean id="multipartResolver" class="org.spr ...