Skip to content

Commit f3bd62e

Browse files
committed
basic
1 parent f943dc1 commit f3bd62e

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

src/MergeTwoSortedLists/Solution.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

src/MinimumPathSum/Solution.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

src/commons/datastructures/ListNode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ public ListNode(int x) {
1212
val = x;
1313
next = null;
1414
}
15+
16+
@Override
17+
public String toString() {
18+
return "ListNode{" +
19+
"val=" + val +
20+
'}';
21+
}
1522
}

src/commons/datastructures/TreeNode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ public class TreeNode {
1212
public TreeNode(int x) {
1313
val = x;
1414
}
15+
16+
@Override
17+
public String toString() {
18+
return "TreeNode{" +
19+
"val=" + val +
20+
'}';
21+
}
1522
}

0 commit comments

Comments
 (0)