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 ...
随机推荐
- 跟我一起学习ASP.NET 4.5 MVC4.0(五)(转)
前面几篇文章介绍了一下ASP.NET MVC中的一些基础,今天我们一起来学习一下在ASP.NET MVC中控件的封装.在页面中我们会经常使用到Html对象,来程序控件,当然这里的控件不是说ASP.NE ...
- 每天一道LeetCode--389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- 关于C# webform 项目发布 aspx页面无任何代码 每个页面都以dll形式发布
关于C# webform 项目发布 注意:aspx页面无任何代码,每个页面都以dll形式发布带bin文件夹里 具体操作方法如下图:
- Linux概述
Linux概述 1.计算机资源 硬件资源 软件资源 硬件资源与软件资源之间的桥梁就是操作系统 2.操作系统分类 Windows :个人版用户最多 Mac :土豪机 Linux :主要应用于服务器 Un ...
- 两对整数明明完全一样,为何一个输出true,一个输出false?&&神奇代码(StrangeIntegerBehavior.java)输出诡异的结果,原因何在
下面有一段代码: public class Main { public static void main(String[] args) { Integer ...
- GDAL中RasterIO函数(把文件读取为一个一维数组)和ReadBlock函数(读取栅格数据块)
CPLErr GDALRasterBand::RasterIO ( GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize, ...
- Java通过代理类实现数据库DAO操作
下面的所有代码示例都取自李兴华的<Java Web开发实战经典>的随书源码,因为觉得设计得很好,所以将代码摘录下来作成笔记. 首先,我们在一个java文件中定义要存储的结构类型: impo ...
- Java--CJDP
was定义,包定义, 1. Java的接口概念 进行封装,方便的使用 2. 包定义,Java 中多种包,进行迁移使用,包的导入,例如对数据库的操作Hibernate 3. 配置文件xml和json,对 ...
- iOS Foundation框架 -2.常用集合类简单总结
Foundation框架中常用的类有:NSString.NSArray.NSSet.NSDictionary 以及它们对应的子类 NSMutableString.NSMutableArray.NSMu ...
- 水仙花数 java 实现
题目描述: 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求 ...