Unique Paths II
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. 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
. 참고: m and n will be at most 100. 코드 는 다음 과 같 습 니 다.
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m=obstacleGrid.length,n=obstacleGrid[0].length;
int[][] matrix=new int[m][n];
int sum=0;
if(obstacleGrid[0][0]==1)
return sum;
matrix[0][0]=1;
boolean flag=false;
for(int i=1;i<n;i++){
if(flag){
matrix[0][i]=0;
}else{
if(obstacleGrid[0][i]==1){
matrix[0][i]=0;
flag=true;
}else{
matrix[0][i]=1;
}
}
}
flag=false;
for(int i=1;i<m;i++){
if(flag){
matrix[i][0]=0;
}else{
if(obstacleGrid[i][0]==1){
matrix[i][0]=0;
flag=true;
}else{
matrix[i][0]=1;
}
}
}
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
if(obstacleGrid[i][j]==1){
matrix[i][j]=0;
}else{
matrix[i][j]=matrix[i-1][j]+matrix[i][j-1];
}
}
}
return matrix[m-1][n-1];
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.