国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 694. Number of Distinct Islands

SunZhaopeng / 2043人閱讀

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("#");
    }
}

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

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72408.html

相關文章

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

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

    pingan8787 評論0 收藏0
  • [LeetCode/LintCode] Number of Islands [DFS]

    摘要:兩個循環(huán)遍歷整個矩陣,出現(xiàn)則將其周圍相鄰的全部標記為,用子函數(shù)遞歸標記。注意里每次遞歸都要判斷邊界。寫一個的,寫熟練。 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 評論0 收藏0
  • leetcode200. Number of Islands

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

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

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

    Raaabbit 評論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 評論0 收藏0

發(fā)表評論

0條評論

SunZhaopeng

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<