zoukankan      html  css  js  c++  java
  • leetcode——63. 不同路径 II

    class Solution:
        def uniquePathsWithObstacles(self, obstacleGrid) -> int:
            if len(obstacleGrid)<1:
                return 0
            m=len(obstacleGrid)
            n=len(obstacleGrid[0])
            if m==1 and n==1:
                if obstacleGrid[0][0]==0:
                    return 1
                else:
                    return 0
            memo=[[0 for i in range(n)] for i in range(m)]#定义空二维数组
            for i in range(n):
                if obstacleGrid[0][i]==1:
                    memo[0][i]=0
                    for k in range(i+1,n):
                        memo[0][k]=0
                    break
                else:
                    memo[0][i]=1
            for j in range(m):
                if obstacleGrid[j][0]==1:
                    memo[j:][0]=[0]*(m-j-1)
                    break
                else:
                    memo[j][0]=1
            for i in range(1,m):
                for j in range(1,n):
                    if obstacleGrid[i][j]==1:
                        memo[i][j]=0
                    else:
                        memo[i][j]=memo[i-1][j]+memo[i][j-1]
            return memo[m-1][n-1]
    执行用时 :64 ms, 在所有 python3 提交中击败了76.80%的用户
    内存消耗 :13.7 MB, 在所有 python3 提交中击败了5.19%的用户
     
                                                      ——2019.10.14
     
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            int m = obstacleGrid.length;
            int n = obstacleGrid[0].length;
            int[][] dp = new int[m][n];
            int i= 0;
            while(i<m){
                if(obstacleGrid[i][0] == 1){
                    dp[i++][0] = 0;
                    while(i<m){
                        dp[i++][0] = 0;
                    }
                }
                if(i<m) {
                    dp[i++][0] = 1;
                }
            }
            int j = 0;
            while(j<n){
                if(obstacleGrid[0][j] == 1){
                    dp[0][j++] = 0;
                    while(j<n){
                        dp[0][j++] = 0;
                    }
                }
                if(j<n) {
                    dp[0][j++] = 1;
                }
            }
    
            for(int i1 = 1;i1<m;i1++){
                for(int j1 = 1;j1<n;j1++){
                    if(obstacleGrid[i1][j1] == 1){
                        dp[i1][j1] = 0;
                    }else {
                        dp[i1][j1] = dp[i1 - 1][j1] + dp[i1][j1 - 1];
                    }
                }
            }
            return dp[m-1][n-1];
        }

     没什么难度。

    ——2020.6.28

     
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    洛谷P3400 仓鼠窝(单调栈)
    牛客练习赛65
    2015 HIAST Collegiate Programming Contest] 题解(AK)
    “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛)
    POJ 2421 Constructing Roads
    逆序数&&线段树
    HDU-1166 敌兵布阵 (线段树&&树状数组入门)
    Codeforces Round #484 (Div. 2)
    HDU
    HDU 5773 The All-purpose Zero (变形LIS)
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11673548.html
Copyright © 2011-2022 走看看