Skip to content

Commit 95ef405

Browse files
committed
2015-12-18 15:55:15
1 parent 201a253 commit 95ef405

15 files changed

Lines changed: 710 additions & 14 deletions

AddBinary.cc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
#include <stack>
7+
#include <vector>
8+
#include <algorithm>
9+
#include <sstream>
10+
11+
12+
13+
using namespace std;
14+
15+
class Solution {
16+
public:
17+
string addBinary(string a, string b) {
18+
if (a.size() == 0) return b;
19+
if (b.size() == 0) return a;
20+
int i = a.size() - 1;
21+
int j = b.size() - 1;
22+
int carry = 0;
23+
vector<int> res;
24+
while (i >= 0 && j >= 0) {
25+
int digit = (int)(a[i] - '0' + b[j] - '0') + carry;
26+
carry = digit / 2;
27+
digit %= 2;
28+
res.push_back(digit);
29+
i--;
30+
j--;
31+
}
32+
while (i >= 0) {
33+
int digit = (int)(a[i] - '0') + carry;
34+
carry = digit / 2;
35+
digit %= 2;
36+
res.push_back(digit);
37+
i--;
38+
}
39+
while (j >= 0) {
40+
int digit = (int)(b[j] - '0') + carry;
41+
carry = digit / 2;
42+
digit %= 2;
43+
res.push_back(digit);
44+
j--;
45+
}
46+
if (carry > 0) {
47+
res.push_back(carry);
48+
}
49+
50+
reverse(res.begin(), res.end());
51+
stringstream stream_;
52+
for (int i = 0; i < res.size(); ++i) {
53+
stream_ << res[i];
54+
}
55+
return stream_.str();
56+
}
57+
};
58+
59+
int main(int argc, char const *argv[]) {
60+
/* code */
61+
Solution sol;
62+
cout << sol.addBinary( "11", "1") << endl;
63+
64+
return 0;
65+
}

BinaryTreeLevelOrderTraversalII.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct TreeNode {
1818

1919
class Solution {
2020
public:
21-
vector<vector<int>> result;
21+
vector<vector<int> > result;
2222

2323
void levelTra(TreeNode *root, int level) {
2424
if (root == NULL)
@@ -32,9 +32,9 @@ class Solution {
3232
levelTra(root->right, level + 1);
3333
}
3434

35-
vector<vector<int>> levelOrderBottom(TreeNode *root) {
35+
vector<vector<int> > levelOrderBottom(TreeNode *root) {
3636
levelTra(root, 0);
37-
return vector<vector<int>>(result.rbegin(), result.rend());
37+
return vector<vector<int> >(result.rbegin(), result.rend());
3838
}
3939
};
4040

BinaryTreePaths.cc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
#include <stack>
7+
#include <vector>
8+
9+
using namespace std;
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<string> result;
21+
vector<int> element;
22+
23+
void binaryTreePathsHelper(TreeNode *root) {
24+
25+
element.push_back(root->val);
26+
if (root->left == NULL && root->right == NULL) {
27+
stringstream ss;
28+
int i;
29+
for (i = 0; i < element.size() - 1; ++i) {
30+
ss << element[i] << "->";
31+
}
32+
ss << element[i];
33+
result.push_back(ss.str());
34+
return;
35+
}
36+
37+
if (root->left == NULL) {
38+
binaryTreePathsHelper(root->right);
39+
element.pop_back();
40+
} else if (root->right == NULL) {
41+
binaryTreePathsHelper(root->left);
42+
element.pop_back();
43+
} else {
44+
binaryTreePathsHelper(root->left);
45+
element.pop_back();
46+
binaryTreePathsHelper(root->right);
47+
element.pop_back();
48+
}
49+
}
50+
51+
vector<string> binaryTreePaths(TreeNode *root) {
52+
result.clear();
53+
element.clear();
54+
if (root == NULL)
55+
return result;
56+
binaryTreePathsHelper(root);
57+
return result;
58+
}
59+
};
60+
61+
int main(int argc, char const *argv[]) {
62+
/* code */
63+
return 0;
64+
}

CountandSay.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
#include <stack>
7+
#include <vector>
8+
#include <algorithm>
9+
#include <sstream>
10+
using namespace std;
11+
12+
class Solution {
13+
public:
14+
string unguarded_convert(const string &say) {
15+
stringstream ss;
16+
int count = 0;
17+
char last = say[0];
18+
19+
for (size_t i = 0; i <= say.size(); ++i) {
20+
if (say[i] == last) {
21+
++count;
22+
} else {
23+
ss << count << last;
24+
count = 1;
25+
last = say[i];
26+
}
27+
}
28+
29+
return ss.str();
30+
}
31+
32+
string countAndSay(int n) {
33+
if (n <= 0)
34+
return string();
35+
36+
string say = "1";
37+
38+
for (int i = 1; i < n; ++i) {
39+
say = unguarded_convert(say);
40+
}
41+
42+
return say;
43+
}
44+
};
45+
46+
int main(int argc, char const *argv[]) {
47+
Solution sol;
48+
return 0;
49+
}

IntersectionofTwoLinkedLists.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
#include <stack>
7+
#include <vector>
8+
9+
using namespace std;
10+
11+
struct ListNode {
12+
int val;
13+
ListNode *next;
14+
ListNode(int x) : val(x), next(NULL) {}
15+
};
16+
17+
class Solution {
18+
public:
19+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
20+
if (headA == NULL || headB == NULL)
21+
return NULL;
22+
ListNode *_cur_a = headA;
23+
ListNode *_cur_b = headB;
24+
25+
ListNode *_tail_a = NULL;
26+
ListNode *_tail_b = NULL;
27+
28+
while (1) {
29+
if (_cur_a == NULL)
30+
_cur_a = headB;
31+
if (_cur_b == NULL)
32+
_cur_b = headA;
33+
if (_cur_a == _cur_b)
34+
return _cur_a;
35+
36+
if (_cur_a->next == NULL)
37+
_tail_a = _cur_a;
38+
if (_cur_b->next == NULL)
39+
_tail_b = _cur_b;
40+
if (_tail_a != NULL && _tail_b != NULL && _tail_a != _tail_b)
41+
return NULL;
42+
_cur_a = _cur_a->next;
43+
_cur_b = _cur_b->next;
44+
}
45+
}
46+
};
47+
48+
int main(int argc, char const *argv[]) {
49+
/* code */
50+
51+
return 0;
52+
}

LengthofLastWord.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <memory.h>
5+
#include <sstream>
6+
#include <vector>
7+
#include <iostream>
8+
#include <limits.h>
9+
10+
using namespace std;
11+
12+
class Solution {
13+
public:
14+
int lengthOfLastWord(const char *s) {
15+
int len = strlen(s);
16+
int sum = 0;
17+
while (s[len - 1] == ' ')
18+
len--;
19+
for (int i = len - 1; i >= 0; i--) {
20+
if (s[i] != ' ')
21+
sum++;
22+
else
23+
break;
24+
}
25+
return sum;
26+
}
27+
};
28+
29+
int main(int argc, char const *argv[]) {
30+
/* code */
31+
return 0;
32+
}

LongestCommonPrefix.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
#include <stack>
7+
#include <vector>
8+
#include <algorithm>
9+
#include <sstream>
10+
using namespace std;
11+
12+
class Solution {
13+
public:
14+
string longestCommonPrefix(vector<string> &strs) {
15+
string rs;
16+
if (strs.empty()) return rs;
17+
int len = strs[0].length();
18+
for (int i = 1; i < strs.size(); i++) {
19+
int j = 0;
20+
for (; j < len && j < strs[i].length() && strs[i - 1][j] == strs[i][j];
21+
j++)
22+
;
23+
len = j;
24+
}
25+
return strs[0].substr(0, len);
26+
}
27+
};
28+
29+
int main(int argc, char const *argv[]) {
30+
/* code */
31+
return 0;
32+
}

MergeSortedArray.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
#include <limits.h>
7+
8+
using namespace std;
9+
10+
class Solution {
11+
public:
12+
void merge(int A[], int m, int B[], int n) {
13+
// Start typing your C/C++ solution below
14+
// DO NOT write int main() function
15+
int index = m + n - 1;
16+
int aIndex = m - 1;
17+
int bIndex = n - 1;
18+
while (0 <= aIndex && 0 <= bIndex) {
19+
if (B[bIndex] > A[aIndex]) {
20+
A[index--] = B[bIndex--];
21+
} else {
22+
A[index--] = A[aIndex--];
23+
}
24+
}
25+
26+
while (0 <= aIndex) {
27+
A[index--] = A[aIndex--];
28+
}
29+
30+
while (0 <= bIndex) {
31+
A[index--] = B[bIndex--];
32+
}
33+
}
34+
};
35+
36+
int main(int argc, char const *argv[]) {
37+
/* code */
38+
return 0;
39+
}

0 commit comments

Comments
 (0)