LeetCode#62. Unique Paths

    xiaoxiao2021-04-17  41

    题目:给定一个m*n的数组,从最左上角到最右下角的路径数(每次只能往右或者往下)难度:Medium思路:定义一个m*n的数组来存储从最左上角到第i行第j列那个单元格的路径数(初始化:数组的第0行第0列初始为0,第0行的其他元素为1,第0列的其他元素为1,然后其他行列的值等它的上和左的路径数之和)代码: public class Solution { public int uniquePaths(int m, int n) { int[][] result = new int[m][n]; result[0][0] = 0; if(m == 1 || n == 1){ return 1; } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(i == 0 && j != 0){ result[i][j] = 1; } if(j == 0 && i != 0){ result[i][j] = 1; } if(i >= 1 && j >= 1){ result[i][j] = result[i-1][j] + result[i][j-1]; } } } return result[m-1][n-1]; } }
    转载请注明原文地址: https://ju.6miu.com/read-674428.html

    最新回复(0)