Skip to content

Commit c946bb1

Browse files
committed
Add 18 more.
1 parent e5c60c2 commit c946bb1

18 files changed

+581
-1
lines changed

Decode_Ways.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int numDecodings(string s) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
int len = s.length();
7+
if (len == 0 || s[0] == '0') {
8+
return 0;
9+
}
10+
vector<int> dp;
11+
for (int i = 0; i <= len; i++) {
12+
dp.push_back(0);
13+
}
14+
dp[1] = dp[0] = 1;
15+
for (int i = 2; i <= len; i++) {
16+
if (s[i - 1] != '0') {
17+
dp[i] += dp[i - 1];
18+
}
19+
int num = (s[i - 2] - '0') * 10 + (s[i - 1] - '0');
20+
if (num >= 10 && num <= 26) {
21+
dp[i] += dp[i - 2];
22+
}
23+
}
24+
return dp[len];
25+
}
26+
};

Gray_Code.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution
2+
{
3+
public:
4+
/*
5+
* for reference: http://en.wikipedia.org/wiki/Gray_code.
6+
*/
7+
vector<int> grayCode (int n) {
8+
vector<int> ret;
9+
int size = 1 << n;
10+
for (int i = 0; i < size; i++) {
11+
ret.push_back((i >> 1) ^ i);
12+
}
13+
return ret;
14+
}
15+
};

Largest_Rectangle_in_Histogram.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int largestRectangleArea(vector<int> &height) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
int len = height.size();
7+
if (len == 0) {
8+
return 0;
9+
}
10+
stack<int> st;
11+
int ans = 0, curHeight, width;
12+
for (int i = 0; i < len; i++) {
13+
while (!st.empty() && height[st.top()] >= height[i]) {
14+
curHeight = height[st.top()];
15+
st.pop();
16+
width = (st.empty() ? i : i - st.top() - 1);
17+
ans = max(ans, curHeight * width);
18+
}
19+
st.push(i);
20+
}
21+
while (!st.empty()) {
22+
curHeight = height[st.top()];
23+
st.pop();
24+
width = (st.empty() ? len : len - st.top() - 1);
25+
ans = max(ans, curHeight * width);
26+
}
27+
return ans;
28+
}
29+
};

Maximal_Rectangle.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
int maximalRectangle(vector<vector<char> > &matrix) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
if (matrix.size() == 0) {
7+
return 0;
8+
}
9+
vector<vector<int> > dp;
10+
vector<int> tmp;
11+
for (int i = 0; i < matrix[0].size(); i++) {
12+
tmp.push_back(0);
13+
}
14+
for (int i = 0; i < matrix.size(); i++) {
15+
dp.push_back(tmp);
16+
if (matrix[i][0] == '1') {
17+
dp[i][0] = 1;
18+
}
19+
for (int j = 1; j < matrix[i].size(); j++) {
20+
if (matrix[i][j] == '1') {
21+
dp[i][j] = dp[i][j - 1] + 1;
22+
}
23+
}
24+
}
25+
int ans = 0;
26+
for (int i = 0; i < matrix.size(); i++) {
27+
for (int j = 0; j < matrix[i].size(); j++) {
28+
int width = matrix[i].size();
29+
for (int k = i; k >= 0; k--) {
30+
if (dp[k][j] == 0) {
31+
break;
32+
}
33+
width = min(width, dp[k][j]);
34+
ans = max(ans, width * (i - k + 1));
35+
}
36+
}
37+
}
38+
return ans;
39+
}
40+
};

Partition_List.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode *partition(ListNode *head, int x) {
12+
// Start typing your C/C++ solution below
13+
// DO NOT write int main() function
14+
ListNode *entry = NULL;
15+
ListNode *pHead = NULL;
16+
ListNode *pTail = NULL;
17+
for (ListNode **pCnt = &head; *pCnt != NULL; ) {
18+
entry = *pCnt;
19+
if (entry->val < x) {
20+
*pCnt = entry->next;
21+
if (pHead == NULL) {
22+
pHead = entry;
23+
pTail = entry;
24+
} else {
25+
pTail->next = entry;
26+
pTail = pTail->next;
27+
}
28+
} else {
29+
pCnt = &(entry->next);
30+
}
31+
}
32+
if (pTail != NULL) {
33+
pTail->next = head;
34+
return pHead;
35+
}
36+
return head;
37+
}
38+
};

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ leetcode
33

44
No voice but big deal.
55

6-
> 39 / 132
6+
> 57 / 132
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int removeDuplicates(int A[], int n) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
if (n == 0) {
7+
return 0;
8+
}
9+
int pos = 0;
10+
for (int i = 1; i < n; i++) {
11+
if (A[i] != A[pos]) {
12+
A[++pos] = A[i];
13+
}
14+
}
15+
return pos + 1;
16+
}
17+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
int removeDuplicates(int A[], int n) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
if (n == 0) {
7+
return 0;
8+
}
9+
int cur_num = A[0], cur_count = 1, ans = 0, pos = 0;
10+
for (int i = 1; i < n; i++) {
11+
while (i < n && A[i] == cur_num) {
12+
i++;
13+
cur_count++;
14+
}
15+
cur_count = min(cur_count, 2);
16+
ans += cur_count;
17+
while (cur_count--) {
18+
A[pos++] = cur_num;
19+
}
20+
if (i < n) {
21+
cur_num = A[i];
22+
cur_count = 1;
23+
} else {
24+
cur_count = 0;
25+
}
26+
}
27+
cur_count = min(cur_count, 2);
28+
ans += cur_count;
29+
while (cur_count--) {
30+
A[pos++] = cur_num;
31+
}
32+
return ans;
33+
}
34+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode *deleteDuplicates(ListNode *head) {
12+
// Start typing your C/C++ solution below
13+
// DO NOT write int main() function
14+
ListNode **pCur = &head;
15+
ListNode *pPre = NULL;
16+
ListNode *entry = NULL;
17+
while (*pCur != NULL) {
18+
entry = *pCur;
19+
if (pPre != NULL && pPre->val == entry->val) {
20+
*pCur = entry->next;
21+
delete entry;
22+
entry = NULL;
23+
} else {
24+
pCur = &(entry->next);
25+
pPre = entry;
26+
}
27+
}
28+
return head;
29+
}
30+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode *deleteDuplicates(ListNode *head) {
12+
// Start typing your C/C++ solution below
13+
// DO NOT write int main() function
14+
ListNode **pCur = &head;
15+
ListNode **helper = pCur;
16+
ListNode *entry = NULL;
17+
ListNode *dummy = NULL;
18+
bool flag;
19+
while (*pCur != NULL) {
20+
entry = *pCur;
21+
flag = false;
22+
helper = &(entry->next);
23+
while (*helper != NULL) {
24+
dummy = *helper;
25+
if (dummy->val != entry->val) {
26+
break;
27+
}
28+
flag = true;
29+
*helper = dummy->next;
30+
delete dummy;
31+
dummy = NULL;
32+
}
33+
if (flag) {
34+
*pCur = entry->next;
35+
delete entry;
36+
entry = NULL;
37+
} else {
38+
pCur = &(entry->next);
39+
}
40+
}
41+
return head;
42+
}
43+
};

0 commit comments

Comments
 (0)