Island perimeter problem

Given a two-dimensional grid map containing 0 and 1, where 1 represents land and 0 represents water.

The cells in the grid are connected horizontally and vertically (not diagonally). The grid is completely surrounded by water, but there happens to be one island (or one or more islands connected by grids representing land).

There is no “lake” (” lake “means the water is inside the island and not connected to the water around the island). A cell is a square with sides of length 1. The grid is rectangular and not more than 100 in width or height. Calculate the circumference of the island.

thinking

  1. Calculating the circumference of the island, which turns into a mathematical problem, is to calculate the boundary between 1 and 0. Each pair of 0 and 1 represents the circumference plus 1.

    1. Find all the ones in the matrix.
    2. Find the number of zeros around each 1, taking into account the possibility that the 1 is on the boundary.

code

class Solution {
    private static final int[] dx = {-1.0.1.0};
    private static final int[] dy = {0.1.0, -1};
    public int islandPerimeter(int[][] grid) {
        int res = 0;
        for(int i=0; i<grid.length; i++){for(int j=0; j<grid[0].length; j++){if(grid[i][j] == 1) {int sum = 0;
                    for(int k=0; k<4; k++){int x = j + dx[k];
                        int y = i + dy[k];
                        if(x<0||y<0||x==grid[0].length||y==grid.length||grid[y][x] == 0){
                            sum += 1; } } res += sum; }}}returnres; }}Copy the code

Reformat the department table

Create table If Not Exists Department (id int, revenue int.month varchar(5))
Truncate table Department
insert into Department (id, revenue, month) values ('1'.'8000'.'Jan')
insert into Department (id, revenue, month) values ('2'.'9000'.'Jan')
insert into Department (id, revenue, month) values ('3'.'10000'.'Feb')
insert into Department (id, revenue, month) values ('1'.'7000'.'Feb')
insert into Department (id, revenue, month) values ('1'.'6000'.'Mar')
Copy the code

Write an SQL query to reformat the table so that the new table has a department ID column and some revenue columns for each month.

The answer:

SELECT 
    id,
    sum(case when month='Jan' then Revenue else null end) as 'Jan_Revenue'.sum(case when month='Feb' then Revenue else null end) as 'Feb_Revenue'.sum(case when month='Mar' then Revenue else null end) as 'Mar_Revenue'.sum(case when month='Apr' then Revenue else null end) as 'Apr_Revenue'.sum(case when month='May' then Revenue else null end) as 'May_Revenue'.sum(case when month='Jun' then Revenue else null end) as 'Jun_Revenue'.sum(case when month='Jul' then Revenue else null end) as 'Jul_Revenue'.sum(case when month='Aug' then Revenue else null end) as 'Aug_Revenue'.sum(case when month='Sep' then Revenue else null end) as 'Sep_Revenue'.sum(case when month='Oct' then Revenue else null end) as 'Oct_Revenue'.sum(case when month='Nov' then Revenue else null end) as 'Nov_Revenue'.sum(case when month='Dec' then Revenue else null end) as 'Dec_Revenue'
FROM Department
GROUP BY id
Copy the code

Knowledge:

The question is to convert rows to columns.

CASE column WHEN value THEN return value ELSE Return value END syntax: If a value exists in a column THEN return a value if not.

IF(EXP,VALUE,VALUE): returns the first VALUE when EXP is True, but returns the second VALUE anyway.