数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. 根Activity启动过程

    --摘自<Android进阶解密> 根Activity启动过程中会涉及4个进程,分别是Zygote进程.Launcher进程.AMS所在进程(SystemServer进程).应用程序进程, ...

  2. Prime Distance POJ - 2689 (数学 素数)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  3. struts2的java.lang.NoSuchMethodException错误

    不久前在学习struts时出现这个错误,在网上搜索了半天,发现答案不一.将其总结如下,以方便大家参考. 1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承 ...

  4. gcc各个版本下载

    http://www.gnu.org/order/ftp.html http://ftp.gnu.org/gnu/gcc/

  5. ubuntu下vim使用方法

    按下's'可对文本进行编辑 按下'ESC'再输入':',之后输入wq是保存再退出,输入q是直接退出.如果是只读read only模式则需要输入'wq!'保存退出.

  6. XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)

    A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...

  7. dagger2 依赖注入

    前言: 当 mvp + dagger2 + rxjava 三者遇上,架构更加清晰,代码更加灵活,巧妙结合. 依赖注入:是为了解耦,达到高内聚低耦合的目的,保证代码的健壮性.灵活性和可维护性. publ ...

  8. Mybatis_3.基于注解的增删改查

    1.实体类User.java public class User { private int id; private String name; private int age; //getter.se ...

  9. priority_queue和sort应用

    #include"iostream" #include"String" #include"stdio.h" #include "s ...

  10. [LeetCode] Smallest Rotation with Highest Score 得到最高分的最小旋转

    Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...