zoukankan      html  css  js  c++  java
  • island-perimeter

    https://leetcode.com/problems/island-perimeter/

    package com.company;
    
    
    import java.util.*;
    
    class Solution {
        public int islandPerimeter(int[][] grid) {
            int ret = 0;
            int[] x = {-1, 0, 1, 0};
            int[] y = {0, 1, 0, -1};
            for (int i=0; i<grid.length; i++) {
                for (int j=0; j<grid[0].length; j++) {
                    if (grid[i][j] == 1) {
                        for (int k=0; k<4; k++) {
                            if (i+x[k] < 0 || i+x[k] >= grid.length ||
                                    j+y[k] < 0 || j+y[k] >= grid[0].length ||
                                    grid[i+x[k]][j+y[k]] == 0) {
                                ret++;
                            }
                        }
                    }
                }
            }
            return ret;
        }
    }
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    
            System.out.println("Hello!");
            Solution solution = new Solution();
    
            // Your Codec object will be instantiated and called as such:
            int[][] grid = {{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}};
            int ret = solution.islandPerimeter(grid);
            System.out.printf("ret:%d
    ", ret);
    
            System.out.println();
    
        }
    
    }
  • 相关阅读:
    [国家集训队] Crash 的文明世界
    [国家集训队] middle
    [正睿集训2021] 构造专练
    [正睿集训2021] LIS
    CF482E ELCA
    UVA
    UVA
    UVA
    UVA
    UVA
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6085614.html
Copyright © 2011-2022 走看看