Java for Leetcode 063 Unique Paths II

Source: Internet
Author: User

Follow up for "Unique Paths":

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

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

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: m and N would be is at most 100.

Problem Solving Ideas:

If you use the solution one in the problem, it will be very complex, so we can modify the solution two, Java implementation is as follows:

public int uniquepathswithobstacles (int[][] obstaclegrid) {        int[] v = new Int[obstaclegrid[0].length];for (int i = 0; i < v.length; i++) if (obstaclegrid[0][i] = = 0) V[i] = 1;elsebreak;for (int i = 1; i < obstaclegrid.length; i++) {if (obstaclegrid[i][0 ] = = 1) v[0] = 0;for (int j = 1; J < V.length, J + +) if (obstaclegrid[i][j] = 1) v[j] = 0;elsev[j] + = v[j-1];} return v[v.length-1];    }

Java for Leetcode 063 Unique Paths II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.