463. Island Perimeter

463. Island Perimeter

class Solution {
	public static int islandPerimeter(int[][] grid) {
		int perimeter = 0;
		for (int i = 0; i < grid.length; i++) {
			for (int j = 0; j < grid[i].length; j++) {
				if (grid[i][j] == 1) {
					if (i - 1 < 0 || grid[i - 1][j] == 0)
						perimeter++;
					if (j - 1 = grid.length || grid[i + 1][j] == 0)
						perimeter++;
					if (j + 1 >= grid[i].length || grid[i][j + 1] == 0)
						perimeter++;
				}

			}
		}
		return perimeter;
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s