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. 反射调用与Lambda表达式调用

    想调用一个方法很容易,直接代码调用就行,这人人都会.其次呢,还可以使用反射.不过通过反射调用的性能会远远低于直接调用——至少从绝对时间上来看的确是这样.虽然这是个众所周知的现象,我们还是来写个程序来验 ...

  2. 自定义View(二)ViewPage广告轮播

    自定义View的第二个学习案例,使用ViewPage实现广告轮播,通过组合现有的View实现效果如下: 有关ViewPage使用可以学习谷歌官方API,和训练案例: 1.使用ViewPage实现屏幕滑 ...

  3. Android Studio使用OpenCV后,使APP不安装OpenCV Manager即可运行

    转载自http://www.cnblogs.com/tail/p/4618790.html 采用静态初始化的方法,可以戳下边的链接查看官方的文档介绍 http://docs.opencv.org/do ...

  4. sql深入理解

    我们做软件开发的,大部分人都离不开跟数据库打交道,特别是erp开发的,跟数据库打交道更是频繁,存储过程动不动就是上千行,如果数据量大,人员流动大,那么我们还能保证下一段时间系统还能流畅的运行吗?我们还 ...

  5. jQuery简单邮箱验证

    function chekmail() { var szReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; ...

  6. html5的自定义data-*属性和jquery的data()方法的使用示例

    人们总喜欢往HTML标签上添加自定义属性来存储和操作数据. 但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它 ...

  7. 继承关系在内存和DB中的映射

    使用 将若干相似的类映射为单表,对拥有许多特殊数据的类使用具体表继承. 对高层次使用类表继承,对低层次使用具体表继承. Single Table Inheritance 在DB中将类继承层次设计为一个 ...

  8. virtualbox安装增强包及配置共享文件夹

       因为需要在host及虚拟机间传输数据,想使用共享文件夹.但是单独设置了共享文件夹后在centos里找不到共享文件夹,看了下要安装增强包.好吧,顺 便也解决下鼠标切换的问题,省的老是按右CTL切换 ...

  9. Rhythmbox中文乱码解决办法

    今天在网络上找到了一个比较好的解决Rhythmbox中文乱码的问题的方法 进入你的音乐文件夹执行如下代码: mid3iconv -e GBK *.mp3 如果没有提示多试几次,有可能系统会提示: py ...

  10. java.util.Vector

    public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, C ...