成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 694. Number of Distinct Islands

SunZhaopeng / 2049人閱讀

Problem

Given a non-empty 2D array grid of 0"s and 1"s, an island is a group of 1"s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.

Example 1:
11000
11000
00011
00011
Given the above grid map, return 1.
Example 2:
11011
10000
00001
11011
Given the above grid map, return 3.

Notice that:
11
1
and
1
11
are considered different island shapes, because we do not consider reflection / rotation.
Note: The length of each dimension in the given grid does not exceed 50.

Solution
class Solution {
    public int numDistinctIslands(int[][] grid) {
        if (grid == null || grid.length == 0) return 0;
        Set set = new HashSet<>();
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    StringBuilder sb = new StringBuilder();
                    dfs(grid, i, j, sb, "start:");
                    grid[i][j] = 0;
                    System.out.println(sb.toString());
                    set.add(sb.toString());
                }
            }
        }
        return set.size();
    }
    
    private void dfs(int[][] grid, int i, int j, StringBuilder sb, String dir) {
        if (i < 0 || i == grid.length || j < 0 || j == grid[0].length || grid[i][j] == 0) return;
        sb.append(dir);
        grid[i][j] = 0;
        dfs(grid, i-1, j, sb, "d");
        dfs(grid, i+1, j, sb, "u");
        dfs(grid, i, j-1, sb, "l");
        dfs(grid, i, j+1, sb, "r");
        sb.append("#");
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72408.html

相關(guān)文章

  • [Leetcode] Number of Islands 島嶼數(shù)量(JavaScript 實(shí)現(xiàn))

    摘要:解題思路標(biāo)零法對(duì)這個(gè)矩陣進(jìn)行循環(huán)訪問每一個(gè)點(diǎn)當(dāng)這個(gè)點(diǎn)等于,島嶼數(shù)量,與其同時(shí)用算法訪問周圍的其他點(diǎn),進(jìn)行同樣的操作且將訪問過且等于的點(diǎn)標(biāo)記為零。版本島嶼數(shù)量搜索右邊搜索左邊搜索下邊搜索上邊 Q: Number of Islands Given a 2d grid map of 1s (land) and 0s (water), count the number of islands. ...

    pingan8787 評(píng)論0 收藏0
  • [LeetCode/LintCode] Number of Islands [DFS]

    摘要:兩個(gè)循環(huán)遍歷整個(gè)矩陣,出現(xiàn)則將其周圍相鄰的全部標(biāo)記為,用子函數(shù)遞歸標(biāo)記。注意里每次遞歸都要判斷邊界。寫一個(gè)的,寫熟練。 Number of Islands Problem Given a boolean/char 2D matrix, find the number of islands. 0 is represented as the sea, 1 is represented as...

    Fourierr 評(píng)論0 收藏0
  • leetcode200. Number of Islands

    摘要:題目要求提供一個(gè)二維數(shù)組表示一張地圖,其中代表陸地,代表海洋。這里使用一個(gè)新的二維數(shù)組來表示對(duì)應(yīng)地圖上的元素屬于哪個(gè)并查集。在合并的時(shí)候先進(jìn)行判斷,如果二者為已經(jīng)相連的陸地,則無需合并,否則將新的二維數(shù)組上的元素指向所在的并查集。 題目要求 Given a 2d grid map of 1s (land) and 0s (water), count the number of isla...

    Zoom 評(píng)論0 收藏0
  • [Leetcode] Number of Islands 島嶼個(gè)數(shù)

    摘要:同時(shí)我們每找到一個(gè),就將其標(biāo)為,這樣就能把整個(gè)島嶼變成。我們只要記錄對(duì)矩陣遍歷時(shí)能進(jìn)入多少次搜索,就代表有多少個(gè)島嶼。 Number of Islands 最新更新的思路,以及題II的解法請?jiān)L問:https://yanjia.me/zh/2018/11/... Given a 2d grid map of 1s (land) and 0s (water), count the nu...

    Raaabbit 評(píng)論0 收藏0
  • [LeetCode] 934. Shortest Bridge

    Problem In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two...

    bingo 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<