062 - Unique Paths

来源:互联网 发布:最新网络流行歌曲 编辑:程序博客网 时间:2024/06/06 12:22

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?


Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.



int uniquePaths(int row, int col){    if (!row || !col) return 0;int tmp[2][100];int *p1 = tmp[0];int *p2 = tmp[1];int i;for (i = 0; i < col; i++) tmp[0][i] = tmp[1][i] = 1;int level, loop;for (level = 1; level < row; level++) {for (loop = 1; loop < col; loop++) {p2[loop] = p1[loop] + p2[loop - 1];}int *swap = p1;p1 = p2;p2 = p1;}return p2[col - 1];}


0 0
原创粉丝点击