Skip to content

Commit c8a5953

Browse files
2 parents 6721671 + 579cd4d commit c8a5953

File tree

12 files changed

+230
-0
lines changed

12 files changed

+230
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test intersection.c
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
struct ListNode {
6+
int val;
7+
struct ListNode *next;
8+
};
9+
10+
static struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
11+
{
12+
struct ListNode *p;
13+
for (p = headA; p->next != NULL; p = p->next) {}
14+
p->next = headB;
15+
16+
bool first = true;
17+
struct ListNode *p0, *p1;
18+
for (p0 = headA, p1 = headA; p1 != NULL && p1->next != NULL; p0 = p0->next, p1 = p1->next->next) {
19+
if (p0 == p1 && !first) {
20+
p0 = headA;
21+
while (p0 != p1) {
22+
p0 = p0->next;
23+
p1 = p1->next;
24+
}
25+
p->next = NULL;
26+
return p0;
27+
}
28+
first = false;
29+
}
30+
31+
p->next = NULL;
32+
return NULL;
33+
}
34+
35+
int main(int argc, char **argv)
36+
{
37+
struct ListNode *headA = malloc(sizeof(*headA));
38+
struct ListNode *headB = malloc(sizeof(*headB));
39+
struct ListNode *common = malloc(sizeof(*common));
40+
41+
headA->val = 1;
42+
headB->val = 2;
43+
common->val = 4;
44+
headA->next = common;
45+
headB->next = common;
46+
common->next = NULL;
47+
48+
struct ListNode *p = getIntersectionNode(headA, headB);
49+
if (p != NULL) {
50+
printf("%d\n", p->val);
51+
}
52+
return 0;
53+
}

167_two_sum_ii/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test two_sum.c

167_two_sum_ii/two_sum.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
/**
5+
* Return an array of size *returnSize.
6+
* Note: The returned array must be malloced, assume caller calls free().
7+
*/
8+
static int* twoSum(int* numbers, int numbersSize, int target, int* returnSize)
9+
{
10+
int i = 0, j = numbersSize - 1;
11+
while (i < j) {
12+
int diff = target - numbers[i];
13+
if (diff < numbers[j]) {
14+
while (i < --j && numbers[j + 1] == numbers[j]) {}
15+
} else if (diff > numbers[j]) {
16+
while (++i < j && numbers[i - 1] == numbers[i]) {}
17+
} else {
18+
*returnSize = 2;
19+
int *indexes = malloc(*returnSize * sizeof(int));
20+
indexes[0] = i + 1;
21+
indexes[1] = j + 1;
22+
return indexes;
23+
}
24+
}
25+
26+
*returnSize = 0;
27+
return NULL;
28+
}
29+
30+
int main(int argc, char **argv)
31+
{
32+
if (argc < 3) {
33+
fprintf(stderr, "Usage: ./test target n1 n2...");
34+
exit(-1);
35+
}
36+
37+
int i, count = argc - 2;
38+
int target = atoi(argv[1]);
39+
int *nums = malloc(count * sizeof(int));
40+
for (i = 0; i < count; i++) {
41+
nums[i] = atoi(argv[i + 2]);
42+
}
43+
44+
int number = 0;
45+
int *indexes = twoSum(nums, count, target, &number);
46+
if (indexes != NULL) {
47+
printf("%d %d\n", indexes[0], indexes[1]);
48+
}
49+
return 0;
50+
}

168_excel_sheet_column_title/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test sheet_column.c
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+
4+
static char *convertToTitle(int n)
5+
{
6+
if (n <= 0) {
7+
return "";
8+
}
9+
10+
char *result = malloc(1024);
11+
int len = 0;
12+
do {
13+
result[len++] = ((n - 1) % 26) + 'A';
14+
n = (n - 1) / 26;
15+
} while (n > 0);
16+
result[len] = '\0';
17+
18+
int i, j;
19+
for (i = 0, j = len - 1; i < j; i++, j--) {
20+
char c = result[i];
21+
result[i] = result[j];
22+
result[j] = c;
23+
}
24+
return result;
25+
}
26+
27+
int main(int argc, char **argv)
28+
{
29+
if (argc != 2) {
30+
fprintf(stderr, "Usage: ./test n\n");
31+
exit(-1);
32+
}
33+
34+
printf("%s\n", convertToTitle(atoi(argv[1])));
35+
return 0;
36+
}

169_majority_element/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test majority.c

169_majority_element/majority.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int majorityElement(int* nums, int numsSize)
5+
{
6+
int i, count = 1;
7+
int major = nums[0];
8+
for (i = 1; i < numsSize; i++) {
9+
if (count == 0) {
10+
major = nums[i];
11+
count++;
12+
} else if (major == nums[i]) {
13+
count++;
14+
} else {
15+
count--;
16+
}
17+
}
18+
19+
return major;
20+
}
21+
22+
int main(int argc, char **argv)
23+
{
24+
if (argc < 2) {
25+
fprintf(stderr, "Usage: ./test target n1 n2...\n");
26+
exit(-1);
27+
}
28+
29+
int i, count = argc - 1;
30+
int *nums = malloc(count * sizeof(int));
31+
for (i = 0; i < count; i++) {
32+
nums[i] = atoi(argv[i + 1]);
33+
}
34+
35+
printf("%d\n", majorityElement(nums, count));
36+
return 0;
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test sheet_column.c
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int titleToNumber(char *s)
5+
{
6+
int n = 0;
7+
while (*s != '\0') {
8+
n *= 26;
9+
n += *s++ - 'A' + 1;
10+
}
11+
12+
return n;
13+
}
14+
15+
int main(int argc, char **argv)
16+
{
17+
if (argc != 2) {
18+
fprintf(stderr, "Usage: ./test ABC\n");
19+
exit(-1);
20+
}
21+
22+
printf("%d\n", titleToNumber(argv[1]));
23+
return 0;
24+
}

0 commit comments

Comments
 (0)