数unique island, 比如

110000

110001

001101

101100

100000

总共两个unique岛,不是四个

方法可以是记录每次新的岛屿搜索的路径,left,right,up,down, 作为标志是否相同的key,存hashset

 package fbOnsite;
import java.util.*;
public class UniqueIsland {
public int countIsland(int[][] grid) {
HashSet<String> set = new HashSet<String>(); for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
if (grid[i][j] != 1) continue;
StringBuilder path = new StringBuilder();
dfs(grid, i, j, path.append('s')); //start
set.add(path.toString());
}
} for(String str : set) {
System.out.println(str);
} return set.size();
} public void dfs(int[][] grid, int i, int j, StringBuilder sb) {
grid[i][j] = 2;
//up
if (i>=1 && grid[i-1][j]==1) dfs(grid, i-1, j, sb.append('u'));
//right
if (j<grid[0].length-1 && grid[i][j+1]==1) dfs(grid, i, j+1, sb.append('r'));
//down
if (i<grid.length-1 && grid[i+1][j]==1) dfs(grid, i+1, j, sb.append('d'));
//left
if (j>=1 && grid[i][j-1]==1) dfs(grid, i, j-1, sb.append('l'));
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UniqueIsland sol = new UniqueIsland();
int[][] grid = new int[][]{{1,1,0,0,0,0},{1,1,0,0,0,1},{0,0,1,1,0,0},{1,0,1,1,0,0,},{1,0,0,0,0,0}};
int res = sol.countIsland(grid);
System.out.println(res);
} }

FB面经 Prepare: Count Unique Island的更多相关文章

  1. FB面经prepare: Count the number of Vector

    给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...

  2. FB面经 Prepare: Largest Island

    Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...

  3. Ruby: Count unique elements and their occurences in an array

    Is there a method in Ruby that takes an array, and counts all unique elements and their occurrences ...

  4. FB面经Prepare: Friends Recommendation

    有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...

  5. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  6. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

  7. FB面经Prepare: Email User

    有一些账号,账号里面有一个或多个email, 如果两个账号有共同的email,则认为这两个账号是同一个人,找出哪些账号是同一个人 输入是这样的:数字是用户,字母是邮箱,有很多人有多个邮箱,找出相同的用 ...

  8. FB面经 Prepare: Make Parentheses valid

    给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...

  9. FB面经Prepare: Bipartite a graph

    input friends relations{{1,2}, {2,3}, {3,4}} 把人分成两拨,每拨人互相不认识, 所以应该是group1{1,3}, group2{2,4} 这道题应该是ho ...

随机推荐

  1. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs

    题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...

  2. Pytorch学习(一)基础语法篇

    how to use pytorch 1.Tensor we can create a tensor just like creating a matrix the default type of a ...

  3. Nginx 自定义404、500、502 页面

    利用nginx的反向代理来实现 服务器404 和500 等状态码的自定义页面 1.nginx配置文件 nginx.conf 配置开启代理错误拦截 和配置页面  下划线部分  http { ...... ...

  4. Java是如何加载资源文件的?(源码解毒)

    上文提到应老板要求开发一个测试工具能方便的加载存于文件中的测试参数,当时考虑既然是测试,把测试参数文件和测试类放在一起岂不是很方便,但是老板说:我的需求是你把测试参数文件放到统一文件夹下比如resou ...

  5. 19.3.20 cmd操作:1.dir查看当前文件夹内的文件;2.alt+space+c关闭cmd窗口

    cmd操作记录: 1.dir:查看当前文件夹内的所有文件: 2.alt+space+c:关闭打开的cmd窗口:

  6. CentOS / RHEL 7 : How to setup yum repository using locally mounted DVD

    1. Mount the RHEL 7 installation media ISO to some directory. For example /mnt : # mount -o loop rhe ...

  7. HBuilder

    什么是HBuilder? HBbuilder是DCloud(数字天堂)推出的一款支持HTML5的WEB开发IDE,主体是由java编写的,它将HTML/JS代码块进行代码封装,达到简单数据形成代码的特 ...

  8. c语言,以单词为单位逆序字符串

    #include "string.h" #include "stdio.h" char * nixu(char *c) { ; int n = strlen(c ...

  9. Python全栈-magedu-2018-笔记10

    第三章 - Python 内置数据结构 集set 约定 set 翻译为集合 collection 翻译为集合类型,是一个大概念 set 可变的.无序的.不重复的元素的集合 set定义 初始化 set( ...

  10. 邮局 100分代码(dfs+多重剪枝)

    蓝桥杯真题-邮局 #include<iostream> #include<algorithm> #include<set> #include<string&g ...