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. JQuery Mobile + Cordova 实战一

    好的,今天给大家继续讲解 JQM 和 Cordova 的结合吧.Cordova 和 Phonegap 反正是一个东西,大家就当做一个是旧版(Phonegap)的一个是新版(Cordova)的就行.不同 ...

  2. 根据数据库内容动态生成html页面

    之前使用了很多方法,但是都很复杂. 项目里包括了数据库的管理页面,对数据库进行修改(新增,插入,删除)等之后,在另一个页面使用. 使用时采用按下相应label弹出所有信息的方法,以html的形式将数据 ...

  3. 如何判断单选按钮radio被选中

    今天要写一个单选按钮选中之后显示某些内容,首要问题就是当选中如何获取到值! html代码如下 <input class="joined" type="radio&q ...

  4. mac下apache的多站点配置

    以下操作均建立在    已经配置好了php环境 从网上搜索了下,后来自己配置了下还是比较简单的! 我的环境目录是在/Library/webServer/Documents 测试的时候可以直接在这里建立 ...

  5. vs怎么创建MVC及理解其含义

    怎么创建MVC项目 一·1.点击 文件à新建à项目à模板àVisua C#(选择 .NET Framework 4.0或以上版本) à选择 MVC 3 Web应用程序 或者MVC 4 Web应用程序à ...

  6. centos安装与基本使用

    1.      插入安装光盘 2.      进入试用 3.      在试用的桌面系统选择安装到硬盘 4.      选择安装语言 5.      选择基本存储或者专门的存储设备 6.      - ...

  7. UI2_视图切换

    // // ViewController.m // UI2_视图切换 // // Created by zhangxueming on 15/7/1. // Copyright (c) 2015年 z ...

  8. WP开发笔记——控件倾斜效果

    创建一个基本的 Windows Phone 应用程序并添加 TiltEffect 类文件. 添加要倾斜的控件的分类. 全局应用 IsTiltEnabled 依赖项属性,以便为所有的指定控件提供倾斜功能 ...

  9. LitJSON使用

    地址:http://lbv.github.io/litjson/docs/quickstart.html LitJSON Quickstart Guide Introduction Quick Sta ...

  10. Mysql多表查询(两张独立表,一张关系表)

    一.数据库设计 1.三个数据表长这样   其中user表记录用户信息,cat主要记录男女性别,mete表是用户id和性别id的对应关系   2.具体数据如下   二.查询目标 查询出所有性别为“男”的 ...