zoukankan      html  css  js  c++  java
  • lintcode-115-不同的路径II

    public class Solution {
        /**
         * @param obstacleGrid: A list of lists of integers
         * @return: An integer
         */
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            // write your code here
            int m = obstacleGrid.length,n=obstacleGrid[0].length;
            int[][] dp = new int[m][n];
    
            for(int i=0;i<m;++i){
                for(int j=0;j<n;++j){
                    if(obstacleGrid[i][j]==1){
                        dp[i][j]=0;
                        continue;
                    }
                    if(i==0&&j==0){
                        dp[i][j]=1;
                        continue;
                    }
                    if(i==0){
                        dp[i][j]=dp[i][j-1];
                        continue;
                    }
                    if(j==0){
                        dp[i][j]=dp[i-1][j];
                        continue;
                    }
                    dp[i][j]=dp[i-1][j]+dp[i][j-1];
                }
            }
            return dp[m-1][n-1];
        }
    }

    public class Solution {    /**     * @param obstacleGrid: A list of lists of integers     * @return: An integer     */    public int uniquePathsWithObstacles(int[][] obstacleGrid) {        // write your code here        int m = obstacleGrid.length,n=obstacleGrid[0].length;        int[][] dp = new int[m][n];
            for(int i=0;i<m;++i){            for(int j=0;j<n;++j){                if(obstacleGrid[i][j]==1){                    dp[i][j]=0;                    continue;                }                if(i==0&&j==0){                    dp[i][j]=1;                    continue;                }                if(i==0){                    dp[i][j]=dp[i][j-1];                    continue;                }                if(j==0){                    dp[i][j]=dp[i-1][j];                    continue;                }                dp[i][j]=dp[i-1][j]+dp[i][j-1];            }        }        return dp[m-1][n-1];    }}

  • 相关阅读:
    洛谷P1330 封锁阳光大学
    洛谷P1341 无序字母对
    Bzoj1059 [ZJOI2007]矩阵游戏
    POJ2337 Catenyms
    Bzoj2342 [Shoi2011]双倍回文
    Bzoj1009 [HNOI2008]GT考试
    Bzoj3670 [Noi2014]动物园
    POJ2406 Power Strings
    POJ 2752 Seek the Name, Seek the Fame
    POJ3522 Slim Span
  • 原文地址:https://www.cnblogs.com/t1314/p/12332419.html
Copyright © 2011-2022 走看看