1. 程式人生 > >694. Number of Distinct Islands

694. Number of Distinct Islands

用set記錄形狀

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int width = grid.size();
        if(width <= 0)
            return 0;
        int length = grid[0].size();
        if(length <= 0)
            return 0;
        set<vector<vector<pair<int
,int>>>> shapes; for(int i = 0;i < width;i++){ for(int j = 0;j < length;j++){ if(grid[i][j] == '1'){ vector<vector<pair> > shape; search(); shapes.insert(shape); } } }
return shapes.size(); } void search(vector<vector<char>>& grid,int sx,int sy,int x,int y,vector<vector<pair>>& shape){ if(x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[i][j] == '0') return; grid[x][y] == '
0'; shape.push_back({sx-x,sy-y}); search(grid,sx,sy,x-1,y,shape); search(grid,sx,sy,x+1,y,shape); search(grid,sx,sy,x,y-1,shape); search(grid,sx,sy,x,y+1,shape); } };

https://blog.csdn.net/magicbean2/article/details/79248704

相關推薦

[leetcode]694. Number of Distinct Islands你究竟有幾個異小島?

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 (horizon

694. Number of Distinct Islands

用set記錄形狀 class Solution { public: int numIslands(vector<vector<char>>& grid) { int width = grid.size(); if(width <

[LeetCode] Number of Distinct Islands II 不同島嶼的個數之二

leetcode and arr sid connected emp after etc sum Given a non-empty 2D array grid of 0‘s and 1‘s, an island is a group of 1‘s (represen

LC 711. Number of Distinct Islands II

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 (horizon

[LeetCode] Number of Distinct Islands 不同島嶼的個數

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 m

LintCode 804: Number of Distinct Islands II (BFS/DFS 難題)

Number of Distinct Islands II 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-di

200. Number of Islands

urn return () second round lan read void ret Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An islan

[leetcode-200-Number of Islands]

class col color log char ace sum edge site Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An island is

[LeetCode] Number of Islands

一個 code emp nec island map const 不用 water Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An island is

Number of Islands II

obj row int tin tran oot add new classic lan A 2d grid map of m rows and n columns is initially filled with water. We may perform an add

島嶼的個數 · Number of Islands

image 時間 思路 情況下 sla 風格 題目 程序 不用 [抄題]: [暴力解法]: 時間分析: 空間分析: [思維問題]: [一句話思路]: [輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入): [畫圖]: [一

[LeetCode] 305. Number of Islands II 島嶼的數量之二

nts body target 行合並 leetcode connect ted find after A 2d grid map of m rows and n columns is initially filled with water. We may perform

200. Number of Islands + 695. Max Area of Island

arr get urn ive bsp pub [] sys r++ Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An island is surrou

Leetcode: 200. Number of Islands (week4 --- medium)

題目 示例 分析 題解 總結 題目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounde

#200 Number of Islands

正確解法:用recursion,會讓碼變得簡單一點。首先,在recursion function的開頭來確定邊界條件,如果有out of bound的情況,就馬上return。再次就是,對於四個方向,都做一次dfs,並且在這裡不需要用if來check邊界條件,因為當我們再次進入這個方法的時候,在方法的最開頭會

LeetCode:200. Number of Islands

在看這篇文章前,你也許想要先看看並查集是如何實現的:https://blog.csdn.net/weixin_43462819/article/details/83626022 這一題是在複習完並查集之後的練手的題目。 題目是這樣的: Given a 2d grid map

LeetCode 200.Number of Islands (島嶼的個數)

題目描述: 給定一個由 '1'(陸地)和 '0'(水)組成的的二維網格,計算島嶼的數量。一個島被水包圍,並且它是通過水平方向或垂直方向上相鄰的陸地連線而成的。你可以假設網格的四個邊均被水包圍。 示例 1: 輸入: 11110 11010 11000 00000 輸出

200. Number of Islands - Medium

ges color pan 進行 || con false 條件 元素 Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An island is surrou

LeetCode 200. Number of Islands (島嶼數量)

原題 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connectin

LeetCode--200. Number of Islands

題目連結:https://leetcode.com/problems/number-of-islands/ 要求計算0-1矩陣中的孤島1數目。舉個例子,在某個位置(i,j)board[i][j]=1,則與它(鄰接)連通的“1”都被算作同一片島嶼,這裡可以使用深度優先搜尋將與之連通的‘1’位置全部