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的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. 在网页中制作icon图标

    用字体在网页中画icon图标 第一步:获取字体资源IconMoon网站https://icomoon.io iconMoon中有很多免费小图标可用,还能设置下载图标的使用属性(通过网站中设立的按钮pr ...

  2. c#获取网页内容的三种方法

    1.webclient WebClient wc = new WebClient(); Stream stm = wc.OpenRead(str_url); StreamReader sr = new ...

  3. 跟我一起学习ASP.NET 4.5 MVC4.0(一)(转)

    由于上面一个项目使用的是ASP.NET4.0 MVC3.0,在招人的时候发现很多人有听说过MVC,但是却是没用过,对MVC也只是一知半解,最近想给团队成员做一个系统的解说,让大家都可以学习一下ASP. ...

  4. Oracle学习笔记1:win7 x64下安装Oracle10g

    oracle 10g在win7x64下的安装: 第一次直接双击setup,出错了…… 可能是兼容性的问题,所以试着 右击setup-->属性-->兼容性-->勾上"以兼容模 ...

  5. Android 联系人字母排序(仿微信)

    现在很多APP只要涉及到联系人的界面,几乎都会采取字母排序以及导航的方式.作为程序猿,这种已经普及的需求还是需要学习的,于是小生开始了在网上默默的学习之路,网上学习的资料质量参差不齐,不过也有很不错的 ...

  6. phpstorm使用svn爆出“cannot load supported formats” 的解决

    从这 http://subversion.apache.org/packages.html  下载svn客户端二进制包后修改phpstorm配置中 svn的指向到你的svn可执行程序即可解决

  7. 另类安装系统——PE工具提取

    1. 在当前系统使用安装工具win$man打开,即pe里集成安装工具 2. 选择安装的磁盘或者分区和引导分区 3. 可以默认下一步 4. 不想更改盘符可以默认下一步 5. 最后完成开始安装部署(还需要 ...

  8. jQuery中append、insertBefore、after与insertAfter方法注意事项

    这里列的是针对初学jQuery者来说容易搞不懂的部分,我在这里把这些方法列了个清单,希望大家能看的懂.如下: 方法 源包装集/字串 目标包装集体 特性描述 A.append(B) B A 若目标包装集 ...

  9. jquery 获得table 行数

    1.获得Table总行数,分别使用了两种方法,结果不同:: 1).$("#table_Id").children("tr").length; 只能获得静态页面t ...

  10. 兼容IE,chrome 等所有浏览器 回到顶部代码

    今天在博客园看到一片帖子回到顶部代码,索性就看了下,但是发现在非IE浏览器下可以运行,在IE浏览器下却运行不了. 故将其代码搬弄过来做了些许修改后,完美支持了IE下的运行. 主要实现功能代码文件: & ...