UniquePathsII.java ( File view )
- By issaczr 2014-10-24
- View(s):52
- Download(s):0
- Point(s): 1
// Author: Li Long, 1988lilong@163.com // Date: Apr 17, 2014 // Source: http://oj.leetcode.com/problems/unique-paths-ii/ // Analysis: http://blog.csdn.net/lilong_dream/article/details/19931301 // 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. // Note: m and n will be at most 100. public class UniquePathsII { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length; // if (m == 0) { // return 0; // } int n = obstacleGrid[0].length; if (obstacleGrid[0][0] == 1) { return 0; } else if (m == 1 && n == 1) { return 1; } int[][] paths = new int[m][n]; for (int i = 0; i < m; ++i) { if (obstacleGrid[i][0] == 1) { while (i < m) { paths[i][0] = 0; ++i; } break; } else { paths[i][0] = 1; } } for (int j = 1; j < n; ++j) { if (obstacleGrid[0][j] == 1) { while (j < n) { paths[0][j] = 0; ++j; } break; } else { paths[0][j] = 1; } } for (int i = 1; i < m; ++i) for (int j = 1; j < n; ++j) { if (obstacleGrid[i][j] == 1) { paths[i][j] = 0; } else { paths[i][j] = paths[i][j - 1] + paths[i - 1][j]; } } return paths[m - 1][n - 1]; } public static void main(String[] args) { int[][] obstacle = new int[][] { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 0, 0 } }; UniquePathsII slt = new UniquePathsII(); int result = slt.uniquePathsWithObstacles(obstacle); System.out.println(result); } }
...
Expand> <Close
Sponsored links
File list
Tips: You can preview the content of files by clicking file names^_^Name | Size | Date |
---|---|---|
0 | 1.97 kB | |
0 | 1.97 kB | |
AddBinary.java | 1.58 kB | 2014-05-09 03:48 |
AddTwoNumbers.java | 2.54 kB | 2014-05-09 03:48 |
BinaryTreePreorderTraversal.java | 1.91 kB | 2014-05-09 03:48 |
ClimbingStairs.java | 811.00 B | 2014-05-09 03:48 |
EvaluateReversePolishNotation.java | 1.51 kB | 2014-05-09 03:48 |
GenerateParentheses.java | 1.15 kB | 2014-05-09 03:48 |
ImplementStrStr.java | 1.00 kB | 2014-05-09 03:48 |
InsertionSortList.java | 1.64 kB | 2014-05-09 03:48 |
IntegerToRoman.java | 923.00 B | 2014-05-09 03:48 |
LengthOfLastWord.java | 1.11 kB | 2014-05-09 03:48 |
LinkedListCycle.java | 1.32 kB | 2014-05-09 03:48 |
LinkedListCycleII.java | 1.28 kB | 2014-05-09 03:48 |
LongestCommonPrefix.java | 916.00 B | 2014-05-09 03:48 |
LongestSubstring.java | 1.79 kB | 2014-05-09 03:48 |
MaximumSubarray.java | 1.10 kB | 2014-05-09 03:48 |
MedianOfTwoSortedArrays.java | 1.68 kB | 2014-05-09 03:48 |
MergeSortedArray.java | 1.12 kB | 2014-05-09 03:48 |
MergeTwoSortedLists.java | 1.84 kB | 2014-05-09 03:48 |
MinimumPathSum.java | 1.19 kB | 2014-05-09 03:48 |
PathSum.java | 1.67 kB | 2014-05-09 03:48 |
PlusOne.java | 1.06 kB | 2014-05-09 03:48 |
Powxn.java | 712.00 B | 2014-05-09 03:48 |
RemoveDuplicatesFromSortedList.java | 1.48 kB | 2014-05-09 03:48 |
RemoveDuplicatesfromSortedArray.java | 1.30 kB | 2014-05-09 03:48 |
RemoveElement.java | 885.00 B | 2014-05-09 03:48 |
RemoveNthNodeFromEnd.java | 1.70 kB | 2014-05-09 03:48 |
ReverseInteger.java | 936.00 B | 2014-05-09 03:48 |
RotateList.java | 1.53 kB | 2014-05-09 03:48 |
SameTree.java | 1.15 kB | 2014-05-09 03:48 |
SearchForARange.java | 1.37 kB | 2014-05-09 03:48 |
SearchInRotatedSortedArray.java | 1.27 kB | 2014-05-09 03:48 |
SearchInsertPosition.java | 1.09 kB | 2014-05-09 03:48 |
SetMatrixZeroes.java | 1.95 kB | 2014-05-09 03:48 |
SingleNumber.java | 935.00 B | 2014-05-09 03:48 |
SortList.java | 1.75 kB | 2014-05-09 03:48 |
Sqrt.java | 907.00 B | 2014-05-09 03:48 |
StringToInteger_atoi.java | 1.46 kB | 2014-05-09 03:48 |
Sum3.java | 1.76 kB | 2014-05-09 03:48 |
TwoSum.java | 2.33 kB | 2014-05-09 03:48 |
UniqueBinarySearchTrees.java | 1.01 kB | 2014-05-09 03:48 |
UniquePaths.java | 976.00 B | 2014-05-09 03:48 |
UniquePathsII.java | 1.84 kB | 2014-05-09 03:48 |
ValidParentheses.java | 1.23 kB | 2014-05-09 03:48 |
ValidateBinarySearchTree.java | 1.44 kB | 2014-05-09 03:48 |
Sponsored links