zoukankan      html  css  js  c++  java
  • [LeetCode 063] Unique Paths II

    Unique Paths II

    • 左上角那个点:有obstacle,则为0;没有obstacle,则为1。
    • 最上一排:有obstacle,则为0;没有obstacle,则为左边元素的值。
    • 最左边一排:有obstacle,则为0;没有obstacle,则为上边元素的值。
    • 其它点:有obstacle,则为0;没有obstacle,则为上边元素和左边元素值得和。

    Implementation

    public class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            int right = obstacleGrid[0].length;
            int down = obstacleGrid.length;
            int[] record = new int[right];
            for (int i = 0; i < down; i++) {
                for (int j = 0; j < right; j++) {
                    if (i == 0 && j == 0) {
                        record[j] = (obstacleGrid[i][j] == 1)? 0: 1;
                    }
                    else if (i == 0) {
                        record[j] = (obstacleGrid[i][j] == 1)? 0: record[j - 1];
                    }
                    else if (j == 0) {
                        record[j] = (obstacleGrid[i][j] == 1)? 0: record[j];
                    }
                    else {
                        record[j] = (obstacleGrid[i][j] == 1)? 0: record[j] + record[j - 1];
                    }
                }
            }
            return record[right - 1];
        }
    }
    
  • 相关阅读:
    机器人
    仙岛求药(一)
    YZM 二分查找
    珠心算测验升级版
    博客正在施工
    【其他】16年12月博客阅读索引^_^
    博客有新家了!
    POJ No.3617【B008】
    POJ No.2386【B007】
    【刷题记录】部分和问题
  • 原文地址:https://www.cnblogs.com/Victor-Han/p/5198394.html
Copyright © 2011-2022 走看看