Skip to content

Commit c94adec

Browse files
committed
leetcode
1 parent a9be922 commit c94adec

File tree

9 files changed

+182
-0
lines changed

9 files changed

+182
-0
lines changed

LeetCode/detectCycle.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <vector>
2+
#include <iostream>
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+
typedef long long LL;
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 *detectCycle(ListNode *head) {
20+
if (head == NULL) return NULL;
21+
ListNode* n1 = head;
22+
ListNode* n2 = head;
23+
24+
while (true) {
25+
n1 = n1->next;
26+
if (n1 == NULL) return NULL;
27+
28+
n2 = n2->next;
29+
if (n2 == NULL) return NULL;
30+
n2 = n2->next;
31+
if (n2 == NULL) return NULL;
32+
33+
if (n1 == n2) {
34+
n1 = head;
35+
while (n1 != n2) {
36+
n1 = n1->next;
37+
n2 = n2->next;
38+
}
39+
return n1;
40+
}
41+
}
42+
}
43+
};
44+
45+
int main() {
46+
return 0;
47+
}

LeetCode/generateMatrix.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <vector>
2+
#include <iostream>
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+
typedef long long LL;
10+
11+
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
12+
13+
class Solution {
14+
public:
15+
vector<vector<int> > generateMatrix(int n) {
16+
vector<vector<int> > res(n, vector<int>(n, 0));
17+
int x = 0, y = -1;
18+
int idx = 0;
19+
FOR(i,1,n*n) {
20+
int tx = x + dir[idx][0], ty = y + dir[idx][1];
21+
if (tx < 0 || tx >= n || ty < 0 || ty >= n || res[tx][ty] != 0) idx = (idx + 1) % 4;
22+
x += dir[idx][0], y += dir[idx][1];
23+
res[x][y] = i;
24+
}
25+
return res;
26+
}
27+
};
28+
29+
int main() {
30+
return 0;
31+
}

LeetCode/generateTrees.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <vector>
2+
#include <iostream>
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+
typedef long long LL;
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<TreeNode *> doit(int lt, int rt) {
21+
vector<TreeNode *> res;
22+
if (lt > rt) {
23+
res.push_back(NULL);
24+
return res;
25+
}
26+
FOR(i,lt,rt) {
27+
vector<TreeNode *> lc = doit(lt, i - 1);
28+
vector<TreeNode *> rc = doit(i + 1, rt);
29+
REP(j1,lc.size()) {
30+
REP(j2,rc.size()) {
31+
TreeNode *t = new TreeNode(i);
32+
t->left = lc[j1];
33+
t->right = rc[j2];
34+
res.push_back(t);
35+
}
36+
}
37+
}
38+
return res;
39+
}
40+
vector<TreeNode *> generateTrees(int n) {
41+
return doit(1, n);
42+
}
43+
};
44+
45+
int main() {
46+
return 0;
47+
}

LeetCode/maxPathSum.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <vector>
2+
#include <iostream>
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+
typedef long long LL;
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+
int mx;
21+
int doit(TreeNode *root) {
22+
if (root == NULL) return 0;
23+
int v = root->val;
24+
int lt = max(0, doit(root->left));
25+
int rt = max(0, doit(root->right));
26+
mx = max(mx, v + lt + rt);
27+
return v + max(lt, rt);;
28+
}
29+
int maxPathSum(TreeNode *root) {
30+
if (root == NULL) return 0;
31+
mx = root->val;
32+
doit(root);
33+
return mx;
34+
}
35+
};
36+
37+
int main() {
38+
return 0;
39+
}

LeetCode/search2.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool search(int A[], int n, int target) {
4+
int lt = 0, rt = n - 1;
5+
while (lt <= rt) {
6+
int mt = (lt + rt) / 2;
7+
if (A[mt] == target) return true;
8+
if (A[lt] < A[mt]) {
9+
if (A[lt] <= target && target < A[mt]) rt = mt - 1;
10+
else lt = mt + 1;
11+
} else if (A[lt] > A[mt]) {
12+
if (A[mt] < target && target <= A[rt]) lt = mt + 1;
13+
else rt = mt - 1;
14+
} else ++lt;
15+
}
16+
return false;
17+
}
18+
};

0 commit comments

Comments
 (0)