Careercup - Facebook面试题 - 5890898499993600
2014-05-01 02:30
原题:
Given a matrix of letters and a word, check if the word is present in the matrix. E,g., suppose matrix is:
a b c d e f
z n a b c f
f g f a b c
and given word is fnz, it is present. However, gng is not since you would be repeating g twice.
You can move in all the directions around an element.
题目:给定一个字母矩阵,给定一个单词,请判断从矩阵某一点出发,能否走出一条路径组成这个单词。每一步可以走8邻接的方向。
解法:这基本就是Leetcode的原题Word Search,DFS搞定。如果矩阵太大的话,可以用BFS防止递归过深造成的栈溢出,不过效率方面就更慢了。
代码:
// http://www.careercup.com/question?id=5890898499993600
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
n = (int)board.size();
if (n == ) {
return false;
}
m = (int)board[].size();
word_len = (int)word.length(); if (word_len == ) {
return true;
} int i, j;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
if(dfs(board, word, i, j, )) {
return true;
}
}
}
return false;
}
private:
int n, m;
int word_len; bool dfs(vector<vector<char> > &board, string &word, int x, int y, int idx) {
static const int d[][] = {
{-, -},
{-, },
{-, +},
{ , -},
{ , +},
{+, -},
{+, },
{+, +}
}; if (x < || x > n - || y < || y > m - ) {
return false;
} if (board[x][y] < 'A' || board[x][y] != word[idx]) {
// already searched here
// letter mismatch here
return false;
} bool res;
if (idx == word_len - ) {
// reach the end of word, success
return true;
} for (int i = ; i < ; ++i) {
board[x][y] -= 'a';
res = dfs(board, word, x + d[i][], y + d[i][], idx + );
board[x][y] += 'a';
if (res) {
return true;
}
}
// all letters will be within [a-z], thus I marked a position as 'searched' by setting them to an invalid value.
// we have to restore the value when the DFS is done, so their values must still be distiguishable.
// therefore, I used an offset value of 'a'.
// this tricky way is to save the extra O(n * m) space needed as marker array. return false;
}
};
Careercup - Facebook面试题 - 5890898499993600的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- oracle中的instr()
INSTR (源字符串, 目标字符串, 起始位置, 匹配序号) 在Oracle/PLSQL中,instr函数返回要截取的字符串在源字符串中的位置.只检索一次,就是说从字符的开始 到字符的结尾就结束. ...
- Linq 合并数据并相加
有几条数据是这样的 Person 123 456 789 Person 321 654 987 想合并成 Person 444 1 ...
- 七、Android学习笔记_JNI hello world
1.需要准备的工具,eclipse,cdt(c++)插件,cygwin(unix)和 android ndk. 在cygwin的etc目录下将ndk的路径引入到profile文件中,可以在cygwin ...
- html meta标签之http-equiv
摘要:HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助正确和精确地显示网页内容.常用的HTTP-EQUIV类型有:Content-Type.Refresh .Expi ...
- FusionCharts 相关知识
FusionCharts1.平均线: <trendLines><line startValue='{0}' toolText='平均线' color='#FF0000' displa ...
- 在iOS中,实现点击搜索结果隐藏搜索结果的方法。
不知道有没有别的什么的好的方法,最近在实现一个需求(点击搜索,然后输入搜索内容,显示搜索出来的结果,然后点击搜索结果,在当前页面显示所点击的结果的详细的信息).遇到的问题是,点击搜索结果的时候,搜索的 ...
- VxWorks 6.9 内核编程指导之读书笔记 -- Singnals
Signals 信号是操作系统用于异常处理和异步控制流的关键.在很多方面,信号相当于软件方面的硬件中的中断.操作系统产生的信号包括总线错误和浮点处理异常.信号也提供了API来管理和产生信号.在应用程序 ...
- css笔记——关于css中写上charset “utf-8”
当css文件中写上 charset "utf-8" 时需要将body和html的样式分开写 例如: html,body{margin:0;padding:0;font-family ...
- 13.mariadb-rhce考试解题思路
1.安装mariadb ①yum install -y mariadb mariadb-server 或者 yum groupinstall -y mariadb 2.备份和还原数据库 ①备份:mys ...
- AngularJS(16)-路由
AngularJS 路由 本章节我们将为大家介绍 AngularJS 路由. AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web ...