Skip to content

Commit af0f756

Browse files
author
applewjg
committed
Update
1 parent 981bcd9 commit af0f756

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

PartitionList.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
Author: Annie Kim, [email protected]
2+
Author: Annie Kim, [email protected] : King, [email protected]
33
Date: Apr 9, 2013
4-
Update: Jul 30, 2013
4+
Update: Oct 7, 2014
55
Problem: Partition List
66
Difficulty: Easy
77
Source: http://leetcode.com/onlinejudge#question_86
@@ -26,6 +26,26 @@
2626
class Solution {
2727
public:
2828
ListNode *partition(ListNode *head, int x) {
29+
ListNode leftdummy(-1);
30+
ListNode rightdummy(-1);
31+
ListNode * lhead = &leftdummy;
32+
ListNode * rhead = &rightdummy;
33+
34+
for(ListNode*cur = head; cur; cur=cur->next){
35+
if(cur->val<x){
36+
lhead->next = cur;
37+
lhead = lhead->next;
38+
}else{
39+
rhead->next = cur;
40+
rhead = rhead->next;
41+
}
42+
}
43+
lhead->next = rightdummy.next;
44+
rhead->next = nullptr;
45+
return leftdummy.next;
46+
}
47+
48+
ListNode *partition_1(ListNode *head, int x) {
2949
ListNode dummy(0), *ins = &dummy, *cur = &dummy;
3050
dummy.next = head;
3151
while (cur->next)
@@ -53,4 +73,4 @@ class Solution {
5373
}
5474
return dummy.next;
5575
}
56-
};
76+
};

TrappingRainWater.h

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
2-
Author: Annie Kim, [email protected]
2+
Author: Annie Kim, [email protected] : King, [email protected]
33
Date: May 25, 2013
4+
Update: Oct 07, 2014
45
Problem: Trapping Rain Water
56
Difficulty: Easy
67
Source: http://leetcode.com/onlinejudge#question_42
@@ -17,27 +18,19 @@ class Solution {
1718
public:
1819
int trap(int A[], int n) {
1920
if (n == 0) return 0;
20-
int left[n];
21-
int right[n];
22-
23-
int lmax = A[0];
24-
for (int i = 0; i < n; ++i)
25-
{
26-
lmax = max(lmax, A[i]);
27-
left[i] = lmax;
28-
}
29-
30-
int rmax = A[n-1];
31-
for (int i = n - 1; i >= 0; --i)
32-
{
33-
rmax = max(rmax, A[i]);
34-
right[i] = rmax;
21+
vector<int> maxLeft(n,0);
22+
vector<int> maxRight(n,0);
23+
maxLeft[0] = A[0];
24+
maxRight[n - 1] = A[n - 1];
25+
for (int i = 1; i < n; ++i) {
26+
maxLeft[i] = max(maxLeft[i - 1], A[i]);
27+
maxRight[n - 1 - i] = max(maxRight[n - i], A[n - 1 - i]);
3528
}
36-
29+
3730
int res = 0;
38-
for (int i = 0; i < n; ++i)
39-
res += min(left[i], right[i]) - A[i];
40-
31+
for (int i = 1; i < n; ++i) {
32+
res += min(maxLeft[i], maxRight[i]) - A[i];
33+
}
4134
return res;
4235
}
4336
};

UniqueBinarySearchTrees.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
2-
Author: Annie Kim, [email protected]
2+
Author: Annie Kim, [email protected] : King, [email protected]
33
Date: Jul 10, 2013
4+
Update: Oct 07, 2014
45
Problem: Unique Binary Search Trees
56
Difficulty: Easy
67
Source: http://leetcode.com/onlinejudge#question_96
@@ -20,6 +21,9 @@
2021
class Solution {
2122
public:
2223
int numTrees(int n) {
24+
return numTrees_2(n);
25+
}
26+
int numTrees_1(int n) {
2327
int dp[n+1];
2428
memset(dp, 0, sizeof(dp));
2529
dp[0] = 1;
@@ -28,4 +32,13 @@ class Solution {
2832
dp[i] += dp[j] * dp[i-j-1];
2933
return dp[n];
3034
}
31-
};
35+
int numTrees_2(int n) {
36+
if (n < 0) return 0;
37+
vector<int> dp(n+1, 0);
38+
dp[0] = 1; dp[1] = 1;
39+
for(int i = 2;i <= n; ++i){
40+
dp[i] = dp[i-1] * (4 * i - 2)/(i + 1);
41+
}
42+
return dp[n];
43+
}
44+
};

0 commit comments

Comments
 (0)