File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
src/main/java/sgyj/leetcode Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 33
33
- [ 746. Min Cost Climbing Stairs] ( https://leetcode.com/problems/min-cost-climbing-stairs/description/ )
34
34
- [ 128. Longest Consecutive Sequence] ( https://leetcode.com/problems/longest-consecutive-sequence/ )
35
35
- [ 322. Coin Change] ( https://leetcode.com/problems/coin-change/description/ )
36
+ - [ 62. Unique Paths] ( https://leetcode.com/problems/unique-paths/description/ )
Original file line number Diff line number Diff line change
1
+ package sgyj .leetcode .yeji .section6 ;
2
+
3
+ // 62. Unique Paths
4
+ /*
5
+ There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]).
6
+ The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
7
+ Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
8
+ The test cases are generated so that the answer will be less than or equal to 2 * 109.
9
+ */
10
+
11
+ public class Solution62 {
12
+ private static int [][] visited ;
13
+ private static int answer = 0 ;
14
+ public static int uniquePaths (int m , int n ) {
15
+
16
+ visited = new int [m ][n ];
17
+ visited [0 ][0 ] = 1 ;
18
+
19
+ for (int i =0 ; i <m ; i ++){
20
+ for (int j =0 ; j <n ; j ++){
21
+ int dx = 0 ;
22
+ int dy = 0 ;
23
+ if (i -1 >=0 ){
24
+ dx = visited [i -1 ][j ];
25
+ }
26
+ if (j -1 >=0 ){
27
+ dy = visited [i ][j -1 ];
28
+ }
29
+ visited [i ][j ] = dx +dy == 0 ? 1 : dx +dy ;
30
+ }
31
+ }
32
+
33
+ return visited [m -1 ][n -1 ];
34
+ }
35
+
36
+ public static void main ( String [] args ) {
37
+ int m = 3 ;
38
+ int n = 2 ;
39
+
40
+ System .out .println (uniquePaths (m ,n ));
41
+
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments