forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/begeekmyfriend/leetcode
- Loading branch information
Showing
12 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test intersection.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
struct ListNode { | ||
int val; | ||
struct ListNode *next; | ||
}; | ||
|
||
static struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) | ||
{ | ||
struct ListNode *p; | ||
for (p = headA; p->next != NULL; p = p->next) {} | ||
p->next = headB; | ||
|
||
bool first = true; | ||
struct ListNode *p0, *p1; | ||
for (p0 = headA, p1 = headA; p1 != NULL && p1->next != NULL; p0 = p0->next, p1 = p1->next->next) { | ||
if (p0 == p1 && !first) { | ||
p0 = headA; | ||
while (p0 != p1) { | ||
p0 = p0->next; | ||
p1 = p1->next; | ||
} | ||
p->next = NULL; | ||
return p0; | ||
} | ||
first = false; | ||
} | ||
|
||
p->next = NULL; | ||
return NULL; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct ListNode *headA = malloc(sizeof(*headA)); | ||
struct ListNode *headB = malloc(sizeof(*headB)); | ||
struct ListNode *common = malloc(sizeof(*common)); | ||
|
||
headA->val = 1; | ||
headB->val = 2; | ||
common->val = 4; | ||
headA->next = common; | ||
headB->next = common; | ||
common->next = NULL; | ||
|
||
struct ListNode *p = getIntersectionNode(headA, headB); | ||
if (p != NULL) { | ||
printf("%d\n", p->val); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test two_sum.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/** | ||
* Return an array of size *returnSize. | ||
* Note: The returned array must be malloced, assume caller calls free(). | ||
*/ | ||
static int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) | ||
{ | ||
int i = 0, j = numbersSize - 1; | ||
while (i < j) { | ||
int diff = target - numbers[i]; | ||
if (diff < numbers[j]) { | ||
while (i < --j && numbers[j + 1] == numbers[j]) {} | ||
} else if (diff > numbers[j]) { | ||
while (++i < j && numbers[i - 1] == numbers[i]) {} | ||
} else { | ||
*returnSize = 2; | ||
int *indexes = malloc(*returnSize * sizeof(int)); | ||
indexes[0] = i + 1; | ||
indexes[1] = j + 1; | ||
return indexes; | ||
} | ||
} | ||
|
||
*returnSize = 0; | ||
return NULL; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc < 3) { | ||
fprintf(stderr, "Usage: ./test target n1 n2..."); | ||
exit(-1); | ||
} | ||
|
||
int i, count = argc - 2; | ||
int target = atoi(argv[1]); | ||
int *nums = malloc(count * sizeof(int)); | ||
for (i = 0; i < count; i++) { | ||
nums[i] = atoi(argv[i + 2]); | ||
} | ||
|
||
int number = 0; | ||
int *indexes = twoSum(nums, count, target, &number); | ||
if (indexes != NULL) { | ||
printf("%d %d\n", indexes[0], indexes[1]); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test sheet_column.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static char *convertToTitle(int n) | ||
{ | ||
if (n <= 0) { | ||
return ""; | ||
} | ||
|
||
char *result = malloc(1024); | ||
int len = 0; | ||
do { | ||
result[len++] = ((n - 1) % 26) + 'A'; | ||
n = (n - 1) / 26; | ||
} while (n > 0); | ||
result[len] = '\0'; | ||
|
||
int i, j; | ||
for (i = 0, j = len - 1; i < j; i++, j--) { | ||
char c = result[i]; | ||
result[i] = result[j]; | ||
result[j] = c; | ||
} | ||
return result; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 2) { | ||
fprintf(stderr, "Usage: ./test n\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("%s\n", convertToTitle(atoi(argv[1]))); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test majority.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static int majorityElement(int* nums, int numsSize) | ||
{ | ||
int i, count = 1; | ||
int major = nums[0]; | ||
for (i = 1; i < numsSize; i++) { | ||
if (count == 0) { | ||
major = nums[i]; | ||
count++; | ||
} else if (major == nums[i]) { | ||
count++; | ||
} else { | ||
count--; | ||
} | ||
} | ||
|
||
return major; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc < 2) { | ||
fprintf(stderr, "Usage: ./test target n1 n2...\n"); | ||
exit(-1); | ||
} | ||
|
||
int i, count = argc - 1; | ||
int *nums = malloc(count * sizeof(int)); | ||
for (i = 0; i < count; i++) { | ||
nums[i] = atoi(argv[i + 1]); | ||
} | ||
|
||
printf("%d\n", majorityElement(nums, count)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test sheet_column.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static int titleToNumber(char *s) | ||
{ | ||
int n = 0; | ||
while (*s != '\0') { | ||
n *= 26; | ||
n += *s++ - 'A' + 1; | ||
} | ||
|
||
return n; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 2) { | ||
fprintf(stderr, "Usage: ./test ABC\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("%d\n", titleToNumber(argv[1])); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test zeroes.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static int trailingZeroes(int n) | ||
{ | ||
return n == 0 ? 0 : trailingZeroes(n / 5); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 2) { | ||
fprintf(stderr, "Usage: ./test n\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("%d\n", trailingZeroes(atoi(argv[1]))); | ||
return 0; | ||
} |