Skip to content

Commit ba24d9c

Browse files
committed
update leetcode
1 parent 16a3607 commit ba24d9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+2423
-0
lines changed

100_gp.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// 1. recursive
2+
3+
4+
class Solution {
5+
public:
6+
bool isSameTree(TreeNode* p, TreeNode* q) {
7+
if(p == NULL && q != NULL){
8+
return false;
9+
}
10+
if(p != NULL && q == NULL){
11+
return false;
12+
}
13+
if(q == NULL && p == NULL){
14+
return true;
15+
}
16+
17+
if(p->val != q->val){
18+
return false;
19+
}
20+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
21+
22+
}
23+
};
24+
25+
// 2.
26+
class Solution {
27+
public:
28+
bool isSameTree(TreeNode* p, TreeNode* q) {
29+
// preorder traversal
30+
stack<TreeNode*> stack_p;
31+
stack<TreeNode*> stack_q;
32+
if(p) stack_p.push(p);
33+
if(q) stack_q.push(q);
34+
35+
while(!stack_p.empty() && !stack_q.empty()){
36+
TreeNode* cur_p=stack_p.top();
37+
TreeNode* cur_q=stack_q.top();
38+
39+
stack_p.pop();
40+
stack_q.pop();
41+
42+
if(cur_p->val!=cur_q->val) return false;
43+
44+
if(cur_p->left) stack_p.push(cur_p->left);
45+
if(cur_q->left) stack_q.push(cur_q->left);
46+
47+
if(stack_p.size() != stack_q.size()) return false;
48+
49+
if(cur_p->right) stack_p.push(cur_p->right);
50+
if(cur_q->right) stack_q.push(cur_q->right);
51+
52+
if(stack_p.size() != stack_q.size()) return false;
53+
}
54+
55+
return stack_p.size() == stack_q.size();
56+
}
57+
};

101_gp.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// recursive
2+
class Solution {
3+
public:
4+
bool isSymmetric(TreeNode* root) {
5+
if(!root)
6+
return true;
7+
return helper(root->left, root->right);
8+
}
9+
10+
bool helper(TreeNode* node1, TreeNode* node2){
11+
if(node1 == NULL && node2 == NULL){
12+
return true;
13+
}
14+
15+
if(node1 == NULL && node2 != NULL){
16+
return false;
17+
}
18+
19+
if(node1 != NULL && node2 == NULL){
20+
return false;
21+
}
22+
23+
if(node1->val != node2-> val){
24+
return false;
25+
}
26+
27+
return node1->val == node2-> val && helper(node1->left, node2->right) && helper(node1->right, node2->left);
28+
29+
}
30+
};
31+
32+
//iterative
33+
/**
34+
* Definition for a binary tree node.
35+
* struct TreeNode {
36+
* int val;
37+
* TreeNode *left;
38+
* TreeNode *right;
39+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
40+
* };
41+
*/
42+
class Solution {
43+
public:
44+
bool isSymmetric(TreeNode* root) {
45+
if(!root)
46+
return true;
47+
queue<TreeNode*> bfs;
48+
bfs.push(root);
49+
bfs.push(root);
50+
51+
while(bfs.size() > 0){
52+
TreeNode* t1 = bfs.front();
53+
bfs.pop();
54+
TreeNode* t2 = bfs.front();
55+
bfs.pop();
56+
if (t1 == NULL && t2 == NULL) continue;
57+
if (t1 == NULL || t2 == NULL) return false;
58+
if (t1->val != t2->val) return false;
59+
bfs.push(t1->left);
60+
bfs.push(t2->right);
61+
bfs.push(t1->right);
62+
bfs.push(t2->left);
63+
}
64+
return true;
65+
}
66+
};

104_gp.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Definition for a binary tree node.
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+
int maxDepth(TreeNode* root) {
13+
if(!root)
14+
return 0;
15+
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
16+
}
17+
};
18+
19+
20+
// iterative
21+
/**
22+
* Definition for a binary tree node.
23+
* struct TreeNode {
24+
* int val;
25+
* TreeNode *left;
26+
* TreeNode *right;
27+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
28+
* };
29+
*/
30+
class Solution {
31+
public:
32+
int maxDepth(TreeNode* root) {
33+
if(root == NULL)
34+
return 0;
35+
36+
int res = 0;
37+
queue<TreeNode *> q;
38+
q.push(root);
39+
while(!q.empty())
40+
{
41+
++ res;
42+
for(int i = 0, n = q.size(); i < n; ++ i)
43+
{
44+
TreeNode *p = q.front();
45+
q.pop();
46+
47+
if(p -> left != NULL)
48+
q.push(p -> left);
49+
if(p -> right != NULL)
50+
q.push(p -> right);
51+
}
52+
}
53+
54+
return res;
55+
}
56+
};

121_gp.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
// best buying up to this point
5+
vector<int> minBuy(prices.size(), 0);
6+
vector<int> maxSell(prices.size(), 0);
7+
if(prices.size() <= 1){
8+
return 0;
9+
}
10+
minBuy[0] = prices[0];
11+
for(int i = 1; i < prices.size(); i++){
12+
minBuy[i] = min(minBuy[i-1], prices[i]);
13+
}
14+
15+
maxSell[prices.size() - 1] = prices[prices.size() - 1];
16+
for(int i = prices.size() - 2; i >= 0; i--){
17+
maxSell[i] = max(maxSell[i+1], prices[i]);
18+
}
19+
20+
int result = 0;
21+
22+
for(int i = 0; i < prices.size() - 1; i++){
23+
int profit = maxSell[i+1] - minBuy[i];
24+
if(profit <= 0)
25+
continue;
26+
27+
result = max(result, profit);
28+
29+
}
30+
31+
return result;
32+
}
33+
};

136_gp.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int singleNumber(vector<int>& nums) {
4+
int a = 0;
5+
for(int i = 0; i < nums.size(); i++){
6+
a ^= nums[i];
7+
}
8+
return a;
9+
}
10+
};

141_gp.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
bool hasCycle(ListNode *head) {
12+
ListNode* fast = head;
13+
ListNode* slow = head;
14+
15+
while(fast != NULL && fast->next != NULL){
16+
17+
fast = fast->next->next;
18+
slow = slow ->next;
19+
20+
if(fast == slow){
21+
return true;
22+
}
23+
24+
}
25+
26+
return false;
27+
}
28+
};

155_gp.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class MinStack {
2+
public:
3+
/** initialize your data structure here. */
4+
struct ListNode{
5+
int val;
6+
int minval; // key record
7+
ListNode* next;
8+
ListNode(int val, int minval): val(val), next(NULL), minval(minval){
9+
10+
}
11+
};
12+
13+
MinStack() {
14+
this->m_header = new ListNode(INT_MAX, INT_MAX);
15+
}
16+
17+
~MinStack() {
18+
19+
}
20+
21+
void push(int x) {
22+
int newmin = this->m_header->next == NULL ? x : min(x, this->m_header->next->minval);
23+
ListNode* newNode = new ListNode(x, newmin);
24+
ListNode* tmp = this->m_header->next;
25+
this->m_header->next = newNode;
26+
newNode->next = tmp;
27+
}
28+
29+
void pop() {
30+
ListNode* tmp = this->m_header->next;
31+
this->m_header->next = this->m_header->next->next;
32+
delete tmp;
33+
}
34+
35+
int top() {
36+
return this->m_header->next->val;
37+
}
38+
39+
int getMin() {
40+
return this->m_header->next->minval;
41+
}
42+
43+
private:
44+
ListNode* m_header;
45+
};
46+
47+
/**
48+
* Your MinStack object will be instantiated and called as such:
49+
* MinStack obj = new MinStack();
50+
* obj.push(x);
51+
* obj.pop();
52+
* int param_3 = obj.top();
53+
* int param_4 = obj.getMin();
54+
*/

167.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& numbers, int target) {
4+
5+
int left = 0;
6+
int right = numbers.size() - 1;
7+
8+
while(left < right){
9+
int sum = numbers[left] + numbers[right];
10+
if(sum > target)
11+
right--;
12+
if(sum == target)
13+
return vector<int>{left + 1, right + 1};
14+
if(sum < target)
15+
left++;
16+
}
17+
}
18+
};

169.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public:
3+
int majorityElement(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
return nums[nums.size() / 2];
6+
}
7+
};

172.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
/*
4+
Example Three
5+
6+
By given number 4617.
7+
8+
5^1 : 4617 ÷ 5 = 923.4, so we get 923 factors of 5
9+
10+
5^2 : 4617 ÷ 25 = 184.68, so we get 184 additional factors of 5
11+
12+
5^3 : 4617 ÷ 125 = 36.936, so we get 36 additional factors of 5
13+
14+
5^4 : 4617 ÷ 625 = 7.3872, so we get 7 additional factors of 5
15+
16+
5^5 : 4617 ÷ 3125 = 1.47744, so we get 1 more factor of 5
17+
18+
5^6 : 4617 ÷ 15625 = 0.295488, which is less than 1, so stop here.
19+
20+
Then 4617! has 923 + 184 + 36 + 7 + 1 = 1151 trailing zeroes.
21+
22+
C/C++ code
23+
*/
24+
25+
int trailingZeroes(int n) {
26+
int result = 0;
27+
for(long long i=5; n/i>0; i*=5){
28+
result += (n/i);
29+
}
30+
return result;
31+
}
32+
};

0 commit comments

Comments
 (0)