Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
begeekmyfriend committed Nov 21, 2017
2 parents 6721671 + 579cd4d commit c8a5953
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 160_intersection_of_two_linked_list/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test intersection.c
53 changes: 53 additions & 0 deletions 160_intersection_of_two_linked_list/intersection.c
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;
}
2 changes: 2 additions & 0 deletions 167_two_sum_ii/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test two_sum.c
50 changes: 50 additions & 0 deletions 167_two_sum_ii/two_sum.c
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;
}
2 changes: 2 additions & 0 deletions 168_excel_sheet_column_title/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test sheet_column.c
36 changes: 36 additions & 0 deletions 168_excel_sheet_column_title/sheet_column.c
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;
}
2 changes: 2 additions & 0 deletions 169_majority_element/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test majority.c
37 changes: 37 additions & 0 deletions 169_majority_element/majority.c
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;
}
2 changes: 2 additions & 0 deletions 171_excel_sheet_column_number/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test sheet_column.c
24 changes: 24 additions & 0 deletions 171_excel_sheet_column_number/sheet_column.c
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;
}
2 changes: 2 additions & 0 deletions 172_factorial_trailing_zeros/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test zeroes.c
18 changes: 18 additions & 0 deletions 172_factorial_trailing_zeros/zeroes.c
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;
}

0 comments on commit c8a5953

Please sign in to comment.