promoting the performance


  • 0
    C

    at the very beginning my code is:

    int islandPerimeter(int** grid, int gridRowSize, int gridColSize)
    {
        int i,j;
        int blocks=0,conj=0;
        for(i=0;i<gridRowSize;i++)
        {
            for(j=0;j<gridColSize;j++)
            {
                if(grid[i][j])
                {
                    blocks++;
                    if(i<gridRowSize-1 && grid[i+1][j])conj++;
                    if(j<gridColSize-1 && grid[i][j+1])conj++;
                }
            }
        }
        return blocks*4-conj*2;
    }
    

    it works well but did take some time. beating about 20% of c submissions.
    then i change the last sentence to:
    return (blocks<<2)-(conj<<1);
    time span did not change,but if i use:
    return ((blocks<<1)-conj)<<1;
    then it beats 70% of c submissions,LOL.


Log in to reply
 

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