[LeetCode-interview algorithm classic-Java implementation] [062-Unique Paths (Unique path)], leetcode -- java
[062-Unique Paths (Unique path )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
A robot is located at the top-left corner ofm x n
Grid (marked 'start' in the dimo-below ).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'finish 'in the dimo-below ).
How many possible unique paths are there?
Above is a 3x7 grid. How many possible unique paths are there?
Note:M and n will be at most 100.
Theme
A robot is located in the upper left corner of the m * n square.
A robot can only go down to the right or both sides of a square. The robot must go to the bottom right corner of the square.
How many unique paths are there.
Note:M and n cannot exceed 100.
Solutions
Typical dynamic planning problems are solved using the dynamic planning method.
Save the result with m * n group.
For elements in array.
1. When x = 0 or y = 0, A [x] [y] = 1;
2. When x> = 1 and y> = 1 there is A [\ x] [\ y] = A [x-1] [y] + A [\ x] [Y-1].
3. The requested node is A [s-1] [n-1].
Code Implementation
Algorithm Implementation class
Public class Solution {public int uniquePaths (int m, int n) {int [] [] result = new int [m] [n]; // The solution of the first column for (int I = 0; I <m; I ++) {result [I] [0] = 1 ;} // The solution of the first line for (int I = 1; I <n; I ++) {result [0] [I] = 1 ;} // solutions for other locations (int I = 1; I <m; I ++) {for (int j = 1; j <n; j ++) {result [I] [j] = result [I-1] [j] + result [I] [j-1];} // return result [m-1] [n-1];}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/4252719]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.