Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c803eba

Browse files
add new folder
1 parent 29ddc39 commit c803eba

File tree

145 files changed

+7036
-0
lines changed

Some content is hidden

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

145 files changed

+7036
-0
lines changed

‎c_solution/1.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdio.h>
2+
3+
int* twoSum(int* nums, int numsSize, int target) {
4+
int i,j;
5+
int *ret = malloc(sizeof(int)*2);
6+
7+
for(i = 0; i < numsSize; i++)
8+
{
9+
for(j = i+1; j < numsSize; j++)
10+
{
11+
if(nums[i] + nums[j] == target)
12+
{
13+
ret[0] = i;
14+
ret[1] = j;
15+
return ret;
16+
}
17+
}
18+
}
19+
}

‎c_solution/100.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdio.h>
2+
#define bool int
3+
#define true 1
4+
#define false 0
5+
struct TreeNode {
6+
7+
int val;
8+
struct TreeNode *left;
9+
struct TreeNode *right;
10+
};
11+
12+
13+
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
14+
{
15+
bool ret = false;
16+
17+
if(p == NULL && q == NULL)
18+
return true;
19+
20+
else if(p != NULL && q != NULL)
21+
{
22+
if(p->val != q->val)
23+
return false;
24+
25+
ret = isSameTree(p->left, q->left)&(isSameTree(p->right, q->right));
26+
return ret;
27+
}
28+
29+
else
30+
return false;
31+
}
32+
33+
34+
#if 0 //first version
35+
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
36+
{
37+
bool ret = false;
38+
if(p == NULL || q == NULL)
39+
return false;
40+
41+
if(p->val != q->val)
42+
return false;
43+
44+
if(p->left == NULL && q->right == NULL)
45+
{
46+
return true;
47+
}
48+
49+
else if(p->left != NULL && q->left != NULL)
50+
{
51+
ret = isSameTree(p->left, q->left);
52+
53+
if(ret == true)
54+
{
55+
if(p->right == NULL && q->right == NULL)
56+
{
57+
return true;
58+
}
59+
else if(p->right != NULL && q->right != NULL)
60+
{
61+
ret = isSameTree(p->right, q->right);
62+
return ret;
63+
}
64+
65+
}
66+
}
67+
else
68+
return false;
69+
}
70+
#endif

‎c_solution/101_un.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "tree.h"
2+
3+
//version 1 recursive
4+
bool symmetric( struct TreeNode* left, struct TreeNode* right )
5+
{
6+
if(!left && !right)
7+
return true;
8+
else if(!left || !right)
9+
return false;
10+
if(left->val != right->val)
11+
return false;
12+
13+
return symmetric(left->left, right->right) && symmetric(left->right, right->left);
14+
}
15+
16+
bool isSymmetric( struct TreeNode * root )
17+
{
18+
if(!root)
19+
return true;
20+
return symmetric(root->left, root->right);
21+
}
22+
23+
//version 2 norecursive
24+
struct TreeNode** fillNext( struct TreeNode** stack, int size , int* newSize)
25+
{
26+
struct TreeNode** t = ( struct TreeNode** )malloc(sizeof( struct TreeNode* )*2*size);
27+
int index = 0;
28+
for(int i = 0; i < size; i++)
29+
{
30+
if(stack[i])
31+
{
32+
t[index++] = stack[i]->left;
33+
t[index++] = stack[i]->right;
34+
}
35+
}
36+
*newSize = index;
37+
return t;
38+
}
39+
40+
bool isSymmetric( struct TreeNode * root )
41+
{
42+
if(!root || (!(root->left) && !(root->right)))
43+
return true;
44+
if(!root->left || !root->right)
45+
return false;
46+
47+
int count = 1;
48+
struct TreeNode **lStack = (struct TreeNode**)malloc(sizeof(struct TreeNode*));
49+
struct TreeNode **rStack = (struct TreeNode**)malloc(sizeof(struct TreeNode*));
50+
lStack[0] = root->left;
51+
rStack[0] = root->right;
52+
53+
while(count)
54+
{
55+
int lIndex = 0, rIndex = 0;
56+
for(int i = 0; i < count; i++)
57+
{
58+
if(!lStack[i] || !rStack[count-i-1])
59+
{
60+
if(!lStack[i] && !rStack[count-i-1])
61+
continue;
62+
return false;
63+
}
64+
if(lStack[i]->val != rStack[count-i-1]->val)
65+
return false;
66+
}
67+
int lCount=0, rCount=0;
68+
lStack = fillNext(lStack, count, &lCount);
69+
rStack = fillNext(rStack, count, &rCount);
70+
if(lCount != rCount) return false;
71+
count = lCount;
72+
}
73+
return true;
74+
}

‎c_solution/104.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
3+
int maxDepth(struct TreeNode* root) {
4+
if(root == NULL)
5+
return 0;
6+
int leftDepth = 0;
7+
int rightDepth = 0;
8+
9+
leftDepth = maxDepth(root->left) + 1;
10+
rightDepth = maxDepth(root->right) + 1;
11+
return leftDepth>rightDepth?leftDepth:rightDepth;
12+
}

‎c_solution/108.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "tree.h"
2+
3+
4+
struct TreeNode* vsortedArrayToBST(int* nums, int start, int end)
5+
{
6+
struct TreeNode* root = malloc(sizeof(struct TreeNode));
7+
root->val = nums[(end-start)/2+start];
8+
9+
if(start == end)
10+
{
11+
root->left = NULL;
12+
root->right = NULL;
13+
}
14+
else if(end - start == 1)
15+
{
16+
root->left = NULL;
17+
root->right = vsortedArrayToBST(nums, start+(end-start)/2+1,end);
18+
}
19+
else
20+
{
21+
root->left = vsortedArrayToBST(nums, start, start+(end-start)/2-1);
22+
root->right = vsortedArrayToBST(nums, start+(end-start)/2+1,end);
23+
}
24+
return root;
25+
}
26+
27+
struct TreeNode* sortedArrayToBST(int* nums, int numsSize)
28+
{
29+
if(!nums || numsSize == 0)
30+
return NULL;
31+
else
32+
return vsortedArrayToBST(nums, 0, numsSize-1);
33+
}
34+
35+
36+
37+
void main()
38+
{
39+
int test[3] = {1,2,3};
40+
struct TreeNode* root = sortedArrayToBST(test, 3);
41+
printf("%d,%d,%d\n", root->val, root->left->val, root->right->val);
42+
43+
}

‎c_solution/109.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "list.h"
2+
3+
struct TreeNode* sortedListToBST(struct ListNode* head)
4+
{
5+
if(!head)
6+
return NULL;
7+
else if(!head->next)
8+
{
9+
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
10+
root->val = head->val;
11+
root->left = NULL;
12+
root->right = NULL;
13+
return root;
14+
}
15+
16+
struct ListNode *slow = head, *fast = head, *prv;
17+
while(fast && fast->next)
18+
{
19+
prv = slow;
20+
slow = slow->next;
21+
fast = fast->next->next;
22+
}
23+
24+
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
25+
26+
prv->next = NULL;
27+
root->val = slow->val;
28+
29+
root->left = sortedListToBST(head);
30+
root->right = sortedListToBST(slow->next);
31+
32+
return root;
33+
}
34+
35+
void main()
36+
{
37+
struct ListNode;
38+
}
39+

‎c_solution/11.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "array.h"
2+
3+
//version 2
4+
//based on that if(a < b)
5+
// then a*b <= (a+1)*(b-1)
6+
// so we can use line 12
7+
int maxArea(int* height, int heightSize) {
8+
int left=0, right=heightSize-1;
9+
int max = 0;
10+
while(left < right)
11+
{
12+
int area = (right-left)*(height[left] < height[right]? height[left++] : height[right--]);
13+
max = max > area? max : area;
14+
}
15+
return max;
16+
}
17+
18+
19+
//version1 time exceed
20+
#define area(hei,len) (hei)*(len)
21+
#define high(a,b) (a)>(b)?(b):(a)
22+
23+
int maxArea(int* height, int heightSize) {
24+
if(!height || heightSize <= 1)
25+
return 0;
26+
27+
int left, right, varea;
28+
int max = 0;
29+
30+
for(left = 0; left < heightSize-2; left++)
31+
{
32+
for(right = left+1; right < heightSize-1; right++)
33+
{
34+
varea = area((height[left], height[right]),(right-left));
35+
if(varea > max)
36+
max = varea;
37+
}
38+
}
39+
return max;
40+
}
41+
42+
void main()
43+
{
44+
int test[4] = {1,4,6,7};
45+
printf("%d\n", maxArea(test, 4));
46+
}
47+

‎c_solution/110.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
bool isBalanced(struct TreeNode* root) {
4+
if(!root)
5+
return true;
6+
7+
int height = maxDepth(root->left) - maxDepth(root->right);
8+
if(height == 0 || height == 1 || height == -1)
9+
return isBalanced(root->left) && isBalanced(root->right);
10+
else
11+
return false;
12+
}
13+
14+
int maxDepth(struct TreeNode* root) {
15+
if(root == NULL)
16+
return 0;
17+
int leftDepth = 0;
18+
int rightDepth = 0;
19+
20+
leftDepth = maxDepth(root->left) + 1;
21+
rightDepth = maxDepth(root->right) + 1;
22+
return leftDepth>rightDepth?leftDepth:rightDepth;
23+
}

‎c_solution/111.c

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 <string.h>
4+
5+
6+
int minDepth(struct TreeNode* root) {
7+
if(!root)
8+
return 0;
9+
if(root->left == NULL && root->right == NULL)
10+
return 1;
11+
else if(root->left && root->right)
12+
return minDepth(root->left)+1>minDepth(root->right)+1?minDepth(root->right)+1:minDepth(root->left)+1;
13+
else if(root->left)
14+
return minDepth(root->left)+1;
15+
else if(root->right)
16+
return minDepth(root->right)+1;
17+
}

‎c_solution/112.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "tree.h"
2+
3+
bool hasPathSum(struct TreeNode* root, int sum) {
4+
if(root == NULL)
5+
return false;
6+
7+
if(!root->left && !root->right && root->val == sum)
8+
return true;
9+
else if(root->left && root->right)
10+
return hasPathSum(root->left, sum-root->val) | hasPathSum(root->right, sum-root->val);
11+
else if(root->right)
12+
return hasPathSum(root->right, sum-root->val);
13+
else if(root->left)
14+
return hasPathSum(root->left, sum-root->val);
15+
else
16+
return false;
17+
}
18+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /