2014-05-02 00:22

题目链接

原题:

Given a matrix consisting of 's and 1's, find the largest connected component consisting of 's.

题目:给定一个01矩阵,找出由1构成的连通分量中最大的一个。

解法:四邻接还是八邻接也不说,我就当四邻接了。这种Flood Fill问题,可以用BFS或者DFS解决。

代码:

 // http://www.careercup.com/question?id=5998719358992384
#include <vector>
using namespace std; class Solution {
public:
int maxConnectedComponent(vector<vector<int> > &v) {
n = (int)v.size();
if (n == ) {
return ;
}
m = (int)v[].size();
if (m == ) {
return ;
} int res, max_res; max_res = ;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
if (!v[i][j]) {
continue;
}
res = ; v[i][j] = ;
++res;
DFS(v, i, j, res);
max_res = res > max_res ? res : max_res;
}
} return max_res;
};
private:
int n, m; void DFS(vector<vector<int> > &v, int i, int j, int &res) {
static const int d[][] = {
{-, },
{ , -},
{+, },
{ , +}
}; int k, x, y;
for (k = ; k < ; ++k) {
x = i + d[k][];
y = j + d[k][];
if (x < || x > n - || y < || y > m - ) {
continue;
}
if (!v[x][y]) {
continue;
}
v[x][y] = ;
++res;
DFS(v, x, y, res);
}
};
};

Careercup - Facebook面试题 - 5998719358992384的更多相关文章

  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. (转)优化js脚本设计,防止浏览器假死

    在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出“脚本运行时间过长“的提示框,如果出现这种情况说明你的脚本已经失控了,必须进行优化. 为什么会出现这种情况呢,我们先来看一下浏览器的内 ...

  2. Log4net记录日志存库并存本地

    <?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...

  3. 解决PHP中file_get_contents抓取网页中文乱码问题

    根据网上有朋友介绍说原因可能是服务器开了GZIP压缩. 下面是用firebug查看我的博客的头信息,Gzip是开了的. 请求头信息原始头信息 代码如下 复制代码 Accept text/html,ap ...

  4. SQL中各数据类型的长度、精度

    在 Microsoft? SQL Server? 中,每个列.局部变量.表达式和参数都有一个相关的数据类型,这是指定对象可持有的数据类型(整型.字符.money 等等)的特性. SQL Server ...

  5. 开始写自己的iOS技术博客了

    2015-09-26 中秋节前夕,开始写自己的iOS开发相关的技术博客,还请广大专业的人士批评指教!欢迎纠错和交流! 在来到北京的第二家公司艾亿新融资本管理的子公司——资配易.由于基本没有加班,也算有 ...

  6. hdu 1203 I NEED A OFFER (0-1背包)

    题意分析:0-1背包变形  递推公式:dp[i] = max(dp[i], 1-(1-dp[i-C])*(1-p)) /* I NEED A OFFER! Time Limit: 2000/1000 ...

  7. java虚拟机理解探索1

    以下内容源于个人对<深入java虚拟机>的理解总结 基本概念: java虚拟机可以指一种抽象规范,也可以指一种具体实现,亦可以指一个java虚拟机实例. 虚拟机生命周期: 一个java虚拟 ...

  8. Mongodb 级联删除查询操作

    ObjRelationPojo表一条记录 public class YpObjRelationPojo implements Serializable { @Id private String id; ...

  9. DOS命令之----Netstat+Task以及相关使用

    作为一个初步接触电脑的人,在学习Android的过程中,遇到各种问题,今天遇到了.这样一个错误提示: The connection to adb is down, and a severe error ...

  10. delphi图形图像开发相关

    ①delphi的图形处理(doc) http://wenku.baidu.com/view/519df09951e79b89680226ee.html ②delphi的图形图像处理(ppt) http ...