Leetcode 463: Island Perimeter

问题描述:
计算一片水域中一个岛的周长

在这里插入图片描述
思路: 根据陆地的位置计算四周(四个方向)水域的数量,即该部分边长,注意矩阵边界的四周需要特别考量

代码如下:

class Solution {
    public int islandPerimeter(int[][] grid) {
        int lowerBound=grid.length-1;    //index of the lowerBound of matrix
        int rightBound=grid[0].length-1;  //index of the rightBound of matrix
        int counter=0;
        for(int i=0; i<=lowerBound; i++){
            for(int j=0; j<=rightBound; j++){
                if(grid[i][j]==1){  //if this is a land
                    boolean up;
                    boolean down;
                    boolean left;
                    boolean right;
                    if(i==0){
                        up=false;  //if at line 0, then up=false(is a water grid)
                    }
                    else{
                        if(grid[i-1][j]==0){
                            up=false;
                        }
                        else{
                            up=true;
                        }
                    }
                    if(i==lowerBound){
                        down=false;  //if at last one, then down=false(is a water grid)
                    }
                    else{
                        if(grid[i+1][j]==0){
                            down=false;
                        }
                        else{
                            down=true;
                        }
                    }
                    if(j==0){
                        left=false; //if at column 0, then left=false(is a water grid)
                    }  
                    else{
                        if(grid[i][j-1]==0){
                            left=false;
                        }
                        else{
                            left=true;
                        }
                    }
                    if(j==rightBound){
                         right=false;  //if at column last, then right=false(is a water grid)
                    } 
                    else{
                        if(grid[i][j+1]==0){
                            right=false;
                        }
                        else{
                            right=true;
                        }
                    }
                    counter=counter+whichCase(up, down, left, right);
                }
            }
        }
        return counter;
    }
    
    private int whichCase(boolean up, boolean down, boolean left, boolean right){
        //true: is a land //false: is a water
        int trueCounter=0;
        int falseCounter=0;
        if (up==true){
            trueCounter++;
        }
        else{
            falseCounter++;
        }
        if (down==true){
            trueCounter++;
        } 
        else {
            falseCounter++;
        }
        if (left==true){
            trueCounter++;
        }  
        else{
            falseCounter++;
        }
        if (right==true){
            trueCounter++;
        }  
        else{
            falseCounter++;
        }
        return falseCounter;
    }
}

时间复杂度: O(mn)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值