zoukankan      html  css  js  c++  java
  • 查并集

    独立岛屿数

    题目来源

    LeetCode 200. Number of Islands

    解题思路

    • 暴力:遍历+dfs/bsf
    • 查并集

    精简解题

    //解法一 dfs
    func numIslands(grid [][]byte) int {
    	ret := 0
    	if len(grid) == 0 {
    		return ret
    	}
    	m, n := len(grid), len(grid[0])
    	for i := 0; i < m; i++ {
    		for j := 0; j < n; j++ {
    			if grid[i][j] == '1' {
    				dfs(grid, i, j)
    				ret++
    			}
    		}
    	}
    	return ret
    }
    
    func dfs(grid [][]byte, i int, j int) {
    	if i < 0 || i >= len(grid) || j < 0 || j >= len(grid[0]) {
    		return
    	}
    	if grid[i][j] == '0' {
    		return
    	}
    	grid[i][j] = '0'
    	dfs(grid, i-1, j)
    	dfs(grid, i+1, j)
    	dfs(grid, i, j-1)
    	dfs(grid, i, j+1)
    }
    
    //解法二 查并集
    var match []int
    
    func find(x int) int {
    	if x != match[x] {
    		match[x] = find(match[x])
    	}
    	return match[x]
    }
    func numIslands(grid [][]byte) int {
    	m := len(grid)
    	if m == 0 {
    		return 0
    	}
    	n := len(grid[0])
    	match = make([]int, m*n)
    	for i := 0; i < m*n; i++ {
    		match[i] = i
    	}
    	for i := 0; i < m; i++ {
    		for j := 0; j < n; j++ {
    			x := i*n + j // 当前坐标
    			if grid[i][j] == '0' {
    				match[x] = -1
    				continue
    			}
    			// 数字x和改行向上的数字比较
    			if i != 0 && grid[i-1][j] == '1' {
    				y := (i-1)*n + j
    				match[find(x)] = find(y)
    			}
    			// 数字x和该列向左的数字比较
    			if j != 0 && grid[i][j-1] == '1' {
    				y := i*n + j - 1
    				match[find(x)] = find(y)
    			}
    		}
    	}
    	res := 0
    	mp := make(map[int]int)
    	for i := 0; i < m*n; i++ {
    		if match[i] == -1 {
    			continue
    		}
    		y := find(i)
    		if mp[y] != 1 {
    			res++
    			mp[y] = 1
    		}
    	}
    	return res
    }
    
  • 相关阅读:
    selenium WebDriver 清空input的方式
    selenium 获取input输入框中的值的方法
    webdriver报不可见元素异常方法总结
    git踩过的坑
    what's the 数据结构
    算法进阶——贪心与动态规划
    what's the 二叉树
    算法基础——列表排序
    算法基础——列表查找
    Flask项目示例目录
  • 原文地址:https://www.cnblogs.com/weiweng/p/12486261.html
Copyright © 2011-2022 走看看