C pointer solution, 7ms


  • 2
    C
    int minPathSum(int **grid, int nRows, int nCols) {
        int*pc, *pu, *pl, *pe, **ppc, **ppu, **ppe;
        for(ppu = grid, ppc = ppu + 1, ppe = ppu + nRows; ppc < ppe; ppu = ppc, ++ppc){
            **ppc += **ppu;
        }
        for(pl = grid[0], pc = pl + 1, pe = pl + nCols; pc < pe; pl = pc, ++pc){
            *pc += *pl;
        }
        for(ppu = grid, ppc = ppu + 1, ppe = ppu + nRows; ppc < ppe; ppu = ppc, ++ppc){
            for(pl = ppc[0], pu = ppu[0] + 1, pc = pl + 1, pe = pl + nCols; pc < pe; pl = pc, ++pc, ++pu){
                *pc += (*pl <= *pu ? *pl : *pu);
            }
        }
        return grid[nRows-1][nCols-1];
    }

Log in to reply
 

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