Leetcode:unique Paths II Problem Solving report

Source: Internet
Author: User

Unique Paths IITotal accepted:31019 Total submissions:110866my submissions QuestionSolution

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.

Soulution 1:

Compared with leetcode:unique Paths problem-solving report, it is just to judge whether the current block, if it is block, directly set D[I][J] is 0 is good is not up. Also within 3 minutes bug free seconds answer, oh yes!

1  Public classSolution {2      Public intUniquepathswithobstacles (int[] obstaclegrid) {3         //11504         if(Obstaclegrid = =NULL|| Obstaclegrid.length = =0|| obstaclegrid[0].length = =0) {5             return 0;6         }7         8         introws =obstaclegrid.length;9         intcols = obstaclegrid[0].length;Ten          One         int[] D =New int[Rows][cols]; A          -          for(inti =0; i < rows; i++) { -              for(intj =0; J < cols; J + +) { theD[I][J] =0; -                 if(Obstaclegrid[i][j] = =1) { -D[I][J] =0; -}Else { +                     if(i = =0&& J = =0) { -D[I][J] =1; +                     } A                      at                     if(I! =0) { -D[I][J] + = d[i-1][j]; -                     } -                      -                     if(J! =0) { -D[I][J] + = d[i][j-1]; in                     } -                 } to             } +         } -          the         returnD[rows-1][cols-1]; *     } $}
View Code

Leetcode:unique Paths II Problem Solving report

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.