成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Unique Paths II

firim / 2605人閱讀

摘要:和完全一樣的做法,只要在初始化首行和首列遇到時置零且即可。對了,數(shù)組其它元素遇到也要置零喏,不過就不要啦。

Problem

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Notice

m and n will be at most 100.

Example

For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.

Note

和Unique Path完全一樣的做法,只要在初始化首行和首列遇到obstacle時置零且break即可。對了,數(shù)組其它元素遇到obstacle也要置零喏,不過就不要break啦。

Solution

二維DP

public class Solution {
    public int uniquePathsWithObstacles(int[][] A) {
        int m = A.length, n = A[0].length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            if (A[i][0] == 0) dp[i][0] = 1;
            else break;
        }
        for (int j = 0; j < n; j++) {
            if (A[0][j] == 0) dp[0][j] = 1;
            else break;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (A[i][j] == 0) dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
}

一維DP

public class Solution {
    public int uniquePathsWithObstacles(int[][] A) {
        int n = A[0].length;
        int[] dp = new int[n];
        dp[0] = 1;
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < n; j++) {
                if (A[i][j] == 1) dp[j] = 0;
                else if (j > 0) dp[j] += dp[j-1];
            }
        }
        return dp[n-1];
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/65791.html

相關(guān)文章

  • [LintCode/LeetCode] Unique Paths

    摘要:簡單的動規(guī)題目,建立數(shù)組。坐標為矩陣的坐標,值為從左上角到這一格的走法總數(shù)。賦初值,最上一行和最左列的所有格子的走法都只有一種,其余格子的走法等于其左邊格子走法與上方格子走法之和。最后,返回即可。 Problem A robot is located at the top-left corner of a m x n grid (marked Start in the diagram ...

    Gu_Yan 評論0 收藏0
  • [LintCode/LeetCode] Combination Sum I & II

    摘要:和唯一的不同是組合中不能存在重復的元素,因此,在遞歸時將初始位即可。 Combination Sum I Problem Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T...

    ThreeWords 評論0 收藏0
  • [LintCode/LeetCode] Intersection of Two Arrays I &

    摘要:先想到的是,其實也可以,只是需要在遍歷的時候,添加到數(shù)組中的數(shù)要掉,略微麻煩了一點。在里跑的時候,也要快一點。另一種類似做法的就快的多了。如果是找出所有包括重復的截距呢 Problem Given two arrays, write a function to compute their intersection. Notice Each element in the result m...

    enda 評論0 收藏0
  • [LintCode/LeetCode] Single Number I & II [位運算]

    摘要:整個過程相當于,直接在和里去掉既是又是的。所以最后返回的,一定是只出現(xiàn)過一次的,而出現(xiàn)兩次的都在里,出現(xiàn)三次的都被消去了。 Single Number I Problem Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Example Given [1,2,2,1,3,4,3], return...

    Drinkey 評論0 收藏0
  • [LintCode/LeetCode] Subsets & Subsets II

    Subsets Problem Given a set of distinct integers, return all possible subsets. Notice Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets. Example ...

    tracy 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<