clean xor solution


  • 0
    D
        public class Solution {
           	public int islandPerimeter(int[][] grid) {
    
    	int row = grid.length;
    	int col = grid[0].length;
    	int perim = 0;
    	for (int i = 0; i < row; ++i) {
    		for (int j = 0; j < col - 1; ++j) {
    			perim += (grid[i][j] ^ grid[i][j + 1]);
    		}
    		perim += grid[i][col - 1] + grid[i][0];
    	}
    
    	for (int i = 0; i < col; ++i) {
    		for (int j = 0; j < row - 1; ++j) {
    			perim += grid[j][i] ^ grid[j + 1][i];
    		}
    		perim += grid[0][i] + grid[row - 1][i] ;
    	}
    
    	return perim;
    
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.