c++ solution bit operation


  • 0
    J
    class Solution {
    public:
        int islandPerimeter(vector<vector<int>>& grid) {
            int toggle = 0; 
            int ans = 0;
            
            //check row by row
            for(int i = 0; i < grid.size(); i++){
                toggle = 0;
                for(int j = 0; j < grid[0].size(); j++){
                    toggle = toggle^grid[i][j];
                    ans += toggle;
                    toggle = grid[i][j];
                }
                
                toggle = toggle^0;
                ans += toggle;
            }
            
            //check column by column
            for(int j = 0; j < grid[0].size(); j++){
                toggle = 0;
                 for(int i = 0; i < grid.size(); i++){
                    toggle = toggle^grid[i][j];
                    ans += toggle;
                    toggle = grid[i][j];
                }
                toggle = toggle^0;
                ans += toggle;
            
            }
            
            return ans;
                
            
        }
    };
    

Log in to reply
 

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