File tree Expand file tree Collapse file tree 4 files changed +104
-0
lines changed Expand file tree Collapse file tree 4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ package MergeTwoSortedLists ;
2
+
3
+ import commons .datastructures .ListNode ;
4
+
5
+ /**
6
+ * User: Danyang
7
+ * Date: 1/20/2015
8
+ * Time: 10:40
9
+ *
10
+ * Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
11
+ */
12
+ public class Solution {
13
+ /**
14
+ * Notice:
15
+ * 1. importance of using pre
16
+ * @param l1
17
+ * @param l2
18
+ * @return
19
+ */
20
+ public ListNode mergeTwoLists (ListNode l1 , ListNode l2 ) {
21
+ ListNode c1 = l1 ;
22
+ ListNode c2 = l2 ;
23
+ ListNode dummy = new ListNode (0 );
24
+ ListNode pre = dummy ;
25
+ while (c1 !=null && c2 !=null ) {
26
+ pre .next = new ListNode (0 );
27
+ ListNode c = pre .next ;
28
+ if (c1 .val <c2 .val ) {
29
+ c .val = c1 .val ;
30
+ c1 = c1 .next ;
31
+ }
32
+ else {
33
+ c .val = c2 .val ;
34
+ c2 = c2 .next ;
35
+ }
36
+ pre = pre .next ;
37
+ }
38
+ if (c1 ==null )
39
+ pre .next = c2 ;
40
+ else
41
+ pre .next = c1 ;
42
+
43
+ return dummy .next ;
44
+ }
45
+
46
+ public static void main (String [] args ) {
47
+ ListNode ret = new Solution ().mergeTwoLists (null , null );
48
+ }
49
+ }
Original file line number Diff line number Diff line change
1
+ package MinimumPathSum ;
2
+
3
+ /**
4
+ * User: Danyang
5
+ * Date: 1/20/2015
6
+ * Time: 10:51
7
+ *
8
+ * Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the
9
+ * sum of all numbers along its path.
10
+
11
+ Note: You can only move either down or right at any point in time.
12
+ */
13
+ public class Solution {
14
+ /**
15
+ * f[i][j] represents the min path to i, j
16
+ * f[i][j] = min(f[i-1][j], f[i][j-1])+grid[i][j]
17
+ * @param grid
18
+ * @return
19
+ */
20
+ public int minPathSum (int [][] grid ) {
21
+ int m = grid .length ;
22
+ if (m ==0 )
23
+ return 0 ;
24
+ int n = grid [0 ].length ;
25
+ if (n ==0 )
26
+ return 0 ;
27
+
28
+ int [][] f = new int [m +1 ][n +1 ];
29
+ // initialize
30
+ for (int i =0 ; i <m +1 ; i ++)
31
+ for (int j =0 ; j <n +1 ; j ++)
32
+ f [i ][j ] = Integer .MAX_VALUE ;
33
+ f [0 ][1 ] = 0 ; // entry
34
+
35
+
36
+ for (int i =1 ; i <m +1 ; i ++)
37
+ for (int j =1 ; j <n +1 ; j ++)
38
+ f [i ][j ] = Math .min (f [i -1 ][j ], f [i ][j -1 ])+grid [i -1 ][j -1 ];
39
+ return f [m ][n ];
40
+ }
41
+ }
Original file line number Diff line number Diff line change @@ -12,4 +12,11 @@ public ListNode(int x) {
12
12
val = x ;
13
13
next = null ;
14
14
}
15
+
16
+ @ Override
17
+ public String toString () {
18
+ return "ListNode{" +
19
+ "val=" + val +
20
+ '}' ;
21
+ }
15
22
}
Original file line number Diff line number Diff line change @@ -12,4 +12,11 @@ public class TreeNode {
12
12
public TreeNode (int x ) {
13
13
val = x ;
14
14
}
15
+
16
+ @ Override
17
+ public String toString () {
18
+ return "TreeNode{" +
19
+ "val=" + val +
20
+ '}' ;
21
+ }
15
22
}
You can’t perform that action at this time.
0 commit comments