Skip to content

Commit a90ce82

Browse files
committed
2015-12-17 21:00:10
1 parent 28370da commit a90ce82

14 files changed

Lines changed: 450 additions & 0 deletions

AddDigits.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
int addDigits(int num) { return (num - 1) % 9 + 1; }
12+
};
13+
14+
int main(int argc, char const *argv[]) {
15+
Solution s;
16+
return 0;
17+
}

ContainsDuplicate.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
set<int> int_set;
12+
bool containsDuplicate(vector<int> &nums) {
13+
for (int i = 0; i < nums.size(); i++) {
14+
int temp = nums[i];
15+
if (int_set.find(temp) != int_set.end())
16+
return true;
17+
int_set.insert(temp);
18+
}
19+
return false;
20+
}
21+
};
22+
23+
int main(int argc, char const *argv[]) {
24+
/* code */
25+
return 0;
26+
}

DeleteNodeinaLinkedList.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
using namespace std;
8+
struct ListNode {
9+
int val;
10+
ListNode *next;
11+
ListNode(int x) : val(x), next(NULL) {}
12+
};
13+
14+
class Solution {
15+
public:
16+
void deleteNode(ListNode *node) {
17+
if (!node)
18+
return;
19+
if (node->next == NULL) {
20+
delete node;
21+
} else {
22+
ListNode *temp = node->next;
23+
node->val = node->next->val;
24+
node->next = node->next->next;
25+
delete temp;
26+
}
27+
}
28+
};
29+
30+
int main(int argc, char const *argv[]) {
31+
Solution sol;
32+
return 0;
33+
}

ExcelSheetColumnNumber.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
int titleToNumber(string s) {
12+
int ans = 0;
13+
int len = s.size();
14+
for (int dx = 0; dx < len; dx++) {
15+
ans = ans * 26 + (s[dx] - 'A' + 1);
16+
}
17+
18+
return ans;
19+
}
20+
};
21+
22+
int main(int argc, char const *argv[]) {
23+
/* code */
24+
return 0;
25+
}

InvertBinaryTree.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
struct TreeNode {
10+
int val;
11+
TreeNode *left;
12+
TreeNode *right;
13+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
14+
};
15+
16+
class Solution {
17+
public:
18+
TreeNode *invertTree(TreeNode *root) {
19+
invert_tree_util(root);
20+
return root;
21+
}
22+
23+
void invert_tree_util(TreeNode *root) {
24+
if (root == NULL)
25+
return;
26+
swap(root->left, root->right);
27+
invert_tree_util(root->left);
28+
invert_tree_util(root->right);
29+
}
30+
};
31+
32+
int main(int argc, char const *argv[]) {
33+
/* code */
34+
return 0;
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
struct TreeNode {
10+
int val;
11+
TreeNode *left;
12+
TreeNode *right;
13+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
14+
};
15+
16+
class Solution {
17+
public:
18+
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
19+
if (root == NULL)
20+
return NULL;
21+
if (root == p || root == q)
22+
return root;
23+
24+
TreeNode *left_lca = lowestCommonAncestor(root->left, p, q);
25+
TreeNode *right_lca = lowestCommonAncestor(root->right, p, q);
26+
27+
if (left_lca && right_lca)
28+
return root;
29+
return (left_lca != NULL) ? left_lca : right_lca;
30+
}
31+
};
32+
33+
int main(int argc, char const *argv[]) {
34+
/* code */
35+
return 0;
36+
}

MajorityElement.cc

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

MaximumDepthofBinaryTree.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
struct TreeNode {
10+
int val;
11+
TreeNode *left;
12+
TreeNode *right;
13+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
14+
};
15+
class Solution {
16+
public:
17+
int maxDepth(TreeNode *root) {
18+
if (root == NULL)
19+
return 0;
20+
21+
int res = 0;
22+
queue<TreeNode *> q;
23+
q.push(root);
24+
while (!q.empty()) {
25+
++res;
26+
for (int i = 0, n = q.size(); i < n; ++i) {
27+
TreeNode *p = q.front();
28+
q.pop();
29+
30+
if (p->left != NULL)
31+
q.push(p->left);
32+
if (p->right != NULL)
33+
q.push(p->right);
34+
}
35+
}
36+
37+
return res;
38+
}
39+
};
40+
41+
int main(int argc, char const *argv[]) {
42+
/* code */
43+
return 0;
44+
}

MoveZeroes.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
#include <vector>
7+
8+
9+
using namespace std;
10+
11+
class Solution {
12+
public:
13+
void moveZeroes(vector<int> &nums) {
14+
15+
if (nums.size() == 0)
16+
return;
17+
18+
int insertPos = 0;
19+
for (int num : nums) {
20+
if (num != 0)
21+
nums[insertPos++] = num;
22+
}
23+
while (insertPos < nums.size()) {
24+
nums[insertPos++] = 0;
25+
}
26+
}
27+
};
28+
29+
int main(int argc, char const *argv[]) {
30+
Solution sol;
31+
vector<int> data;
32+
data.push_back(0);
33+
data.push_back(1);
34+
data.push_back(0);
35+
data.push_back(3);
36+
data.push_back(12);
37+
sol.moveZeroes(data);
38+
39+
for (int num : data) {
40+
cout << num << " ";
41+
}cout << endl;
42+
43+
return 0;
44+
}

NimGame.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <memory.h>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
bool canWinNim(int n) { return (n % 4 == 0) ? false : true; }
12+
};
13+
14+
int main(int argc, char const *argv[]) {
15+
Solution sol;
16+
cout << sol.canWinNim(100) << endl;
17+
return 0;
18+
}

0 commit comments

Comments
 (0)