Skip to content

Commit c60596c

Browse files
committed
leetcode 2/15/2014
1 parent f39261e commit c60596c

7 files changed

Lines changed: 257 additions & 0 deletions

File tree

LeetCode/addBinary.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <deque>
4+
#include <algorithm>
5+
using namespace std;
6+
7+
#define REP(i,n) for(int i=0;i<(n);++i)
8+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
9+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
10+
11+
class Solution {
12+
public:
13+
string addBinary(string a, string b) {
14+
if (a.length() > b.length()) swap(a, b);
15+
int la = a.length(), lb = b.length();
16+
int add = 0;
17+
REP(i,la) {
18+
int s = a[la - 1 - i] - '0' + b[lb - 1 - i] - '0' + add;
19+
add = s / 2;
20+
s %= 2;
21+
b[lb - 1 - i] = s + '0';
22+
}
23+
FOR(i,la,lb-1) {
24+
int s = b[lb - 1 - i] - '0' + add;
25+
add = s / 2;
26+
s %= 2;
27+
b[lb - 1 - i] = s + '0';
28+
if (add == 0) break;
29+
}
30+
if (add == 1) b = "1" + b;
31+
return b;
32+
}
33+
};
34+
35+
int main() {
36+
return 0;
37+
}

LeetCode/isInterleave.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <deque>
4+
#include <algorithm>
5+
using namespace std;
6+
7+
#define REP(i,n) for(int i=0;i<(n);++i)
8+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
9+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
10+
11+
class Solution {
12+
public:
13+
bool isInterleave(string s1, string s2, string s3) {
14+
int l1 = s1.length(), l2 = s2.length(), l3 = s3.length();
15+
if (l1 + l2 != l3) return false;
16+
if (l1 == 0) return s2 == s3;
17+
if (l2 == 0) return s1 == s3;
18+
19+
vector<vector<int> > mm(l1 + 1, vector<int>(l2 + 1, 0));
20+
if (s3[l3 - 1] == s2[l2 - 1]) mm[l1][l2 - 1] = 1;
21+
if (s3[l3 - 1] == s1[l1 - 1]) mm[l1 - 1][l2] = 1;
22+
23+
RFOR(idx,l3-2,0) {
24+
FOR(i,0,idx+1) {
25+
if (i > l1) break;
26+
int j = idx + 1 - i;
27+
if (j > l2) continue;
28+
if (j < 0) break;
29+
if (mm[i][j] == 0) continue;
30+
if (i - 1 >= 0 && s3[idx] == s1[i - 1]) mm[i - 1][j] = 1;
31+
if (j - 1 >= 0 && s3[idx] == s2[j - 1]) mm[i][j - 1] = 1;
32+
}
33+
}
34+
35+
return mm[0][0] == 1;
36+
}
37+
};
38+
39+
int main() {
40+
Solution s;
41+
cout << s.isInterleave("aabcc", "dbbca", "aadbbcbcac") << endl;
42+
cout << s.isInterleave("aabcc", "dbbca", "aadbbbaccc") << endl;
43+
return 0;
44+
}

LeetCode/isValidBST.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
struct TreeNode {
7+
int val;
8+
TreeNode *left;
9+
TreeNode *right;
10+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11+
};
12+
13+
class Solution {
14+
public:
15+
bool isLess(TreeNode *root, int num) {
16+
if (root == NULL) return true;
17+
if (root->val >= num) return false;
18+
return isLess(root->left, num) && isLess(root->right, num);
19+
}
20+
bool isMore(TreeNode *root, int num) {
21+
if (root == NULL) return true;
22+
if (root->val <= num) return false;
23+
return isMore(root->left, num) && isMore(root->right, num);
24+
}
25+
bool isValidBST(TreeNode *root) {
26+
if (root == NULL) return true;
27+
28+
if (!isLess(root->left, root->val)) return false;
29+
if (!isMore(root->right, root->val)) return false;
30+
31+
return isValidBST(root->left) && isValidBST(root->right);
32+
}
33+
};
34+
35+
int main() {
36+
return 0;
37+
}

LeetCode/levelOrder.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for binary tree
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
vector<vector<int> > levelOrder(TreeNode *root) {
13+
vector<vector<int> > res;
14+
if (root == NULL) return res;
15+
deque<pair<TreeNode, int> > mm;
16+
mm.push_back(make_pair(*root, 0));
17+
while (!mm.empty()) {
18+
TreeNode now = mm.front().first;
19+
int idx = mm.front().second;
20+
mm.pop_front();
21+
if (res.size() <= idx) res.push_back(vector<int>());
22+
res[idx].push_back(now.val);
23+
if (now.left != NULL) mm.push_back(make_pair(*now.left, idx + 1));
24+
if (now.right != NULL) mm.push_back(make_pair(*now.right, idx + 1));
25+
}
26+
return res;
27+
}
28+
};

LeetCode/levelOrderBottom.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <deque>
4+
#include <algorithm>
5+
using namespace std;
6+
7+
#define REP(i,n) for(int i=0;i<(n);++i)
8+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
9+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
10+
11+
struct TreeNode {
12+
int val;
13+
TreeNode *left;
14+
TreeNode *right;
15+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
16+
};
17+
18+
class Solution {
19+
public:
20+
vector<vector<int> > levelOrderBottom(TreeNode *root) {
21+
vector<vector<int> > res;
22+
if (root == NULL) return res;
23+
deque<pair<TreeNode, int> > mm;
24+
mm.push_back(make_pair(*root, 0));
25+
while (!mm.empty()) {
26+
TreeNode now = mm.front().first;
27+
int idx = mm.front().second;
28+
mm.pop_front();
29+
if (res.size() <= idx) res.push_back(vector<int>());
30+
res[idx].push_back(now.val);
31+
if (now.left != NULL) mm.push_back(make_pair(*now.left, idx + 1));
32+
if (now.right != NULL) mm.push_back(make_pair(*now.right, idx + 1));
33+
}
34+
reverse(res.begin(), res.end());
35+
return res;
36+
}
37+
};
38+
39+
int main() {
40+
return 0;
41+
}

LeetCode/postorderTraversal.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
#define REP(i,n) for(int i=0;i<(n);++i)
7+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
8+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
9+
10+
struct TreeNode {
11+
int val;
12+
TreeNode *left;
13+
TreeNode *right;
14+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
15+
};
16+
17+
class Solution {
18+
public:
19+
vector<int> postorderTraversal(TreeNode *root) {
20+
vector<TreeNode> mm;
21+
vector<int> res;
22+
if (root == NULL) return res;
23+
24+
mm.push_back(*root);
25+
while (!mm.empty()) {
26+
TreeNode now = mm.back();
27+
mm.pop_back();
28+
res.push_back(now.val);
29+
if (now.left != NULL) mm.push_back(*now.left);
30+
if (now.right != NULL) mm.push_back(*now.right);
31+
}
32+
33+
reverse(res.begin(), res.end());
34+
return res;
35+
}
36+
};
37+
38+
int main() {
39+
return 0;
40+
}

LeetCode/validPalindrome.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <deque>
4+
#include <algorithm>
5+
using namespace std;
6+
7+
#define REP(i,n) for(int i=0;i<(n);++i)
8+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
9+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
10+
11+
class Solution {
12+
public:
13+
bool isPalindrome(string s) {
14+
string str = "";
15+
REP(i,s.length()) {
16+
if (s[i] >= '0' && s[i] <= '9') str += s[i];
17+
else if (s[i] >= 'a' && s[i] <= 'z') str += s[i];
18+
else if (s[i] >= 'A' && s[i] <= 'Z') str += (s[i] - 'A' + 'a');
19+
}
20+
int len = str.length();
21+
REP(i,(len/2)) {
22+
if (str[i] != str[len - 1 - i]) return false;
23+
}
24+
return true;
25+
}
26+
};
27+
28+
int main() {
29+
return 0;
30+
}

0 commit comments

Comments
 (0)