From 3b664915d0d622817d51463f8a3ac96793cb808f Mon Sep 17 00:00:00 2001 From: begeekmyfriend Date: Wed, 12 Jul 2017 13:56:03 +0800 Subject: [PATCH] Two sum Signed-off-by: begeekmyfriend --- 001_two_sum/Makefile | 2 + 001_two_sum/two_sum.c | 261 +++++++++++++++++------------------------- 2 files changed, 106 insertions(+), 157 deletions(-) create mode 100644 001_two_sum/Makefile diff --git a/001_two_sum/Makefile b/001_two_sum/Makefile new file mode 100644 index 0000000..ffb6456 --- /dev/null +++ b/001_two_sum/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test two_sum.c diff --git a/001_two_sum/two_sum.c b/001_two_sum/two_sum.c index 69b37aa..41fe752 100644 --- a/001_two_sum/two_sum.c +++ b/001_two_sum/two_sum.c @@ -1,197 +1,144 @@ -/* - * Copyright (C) 2015, Leo Ma - */ - #include #include #include struct hlist_node; -struct hlist_head -{ - struct hlist_node *first; +struct hlist_head { + struct hlist_node *first; }; -struct hlist_node -{ - struct hlist_node *next, **pprev; +struct hlist_node { + struct hlist_node *next, **pprev; }; -static inline void -INIT_HLIST_HEAD(struct hlist_head *h) -{ - h->first = NULL; -} - -static inline void -INIT_HLIST_NODE(struct hlist_node *n) -{ - n->next = NULL; - n->pprev = NULL; -} - -static inline int -hlist_unhashed(struct hlist_node *n) -{ - return !n->pprev; +static inline void INIT_HLIST_HEAD(struct hlist_head *h) { + h->first = NULL; } -static inline int -hlist_empty(struct hlist_head *h) -{ - return !h->first; +static inline void INIT_HLIST_NODE(struct hlist_node *n) { + n->next = NULL; + n->pprev = NULL; } -static inline void -hlist_add_head(struct hlist_node *n, struct hlist_head *h) -{ - if (h->first != NULL) { - h->first->pprev = &n->next; - } - n->next = h->first; - n->pprev = &h->first; - h->first = n; +static inline int hlist_empty(struct hlist_head *h) { + return !h->first; } -static inline void -__hlist_del(struct hlist_node *n) +static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) { - struct hlist_node *next = n->next; - struct hlist_node **pprev = n->pprev; - *pprev = next; - if (next != NULL) { - next->pprev = pprev; - } + if (h->first != NULL) { + h->first->pprev = &n->next; + } + n->next = h->first; + n->pprev = &h->first; + h->first = n; } -static inline void -hlist_del(struct hlist_node *n) +static inline void hlist_del(struct hlist_node *n) { - __hlist_del(n); -} - -static inline void -hlist_add_before(struct hlist_node *n, struct hlist_node *next) -{ - n->next = next; - n->pprev = next->pprev; - *(n->pprev) = n; - next->pprev = &n->next; -} - -static inline void -hlist_add_behind(struct hlist_node *n, struct hlist_node *prev) -{ - struct hlist_node *next = prev->next; - n->next = next; - prev->next = n; - n->pprev = &prev->next; - if (next != NULL) { - next->pprev = &n->next; - } + struct hlist_node *next = n->next; + struct hlist_node **pprev = n->pprev; + *pprev = next; + if (next != NULL) { + next->pprev = pprev; + } } #define container_of(ptr, type, member) \ - ((type *)((char *)(ptr) - (size_t)&(((type *)0)->member))) + ((type *)((char *)(ptr) - (size_t)&(((type *)0)->member))) #define hlist_entry(ptr, type, member) \ - container_of(ptr, type, member) + container_of(ptr, type, member) #define hlist_for_each(pos, head) \ - for (pos = (head)->first; pos; pos = pos->next) + for (pos = (head)->first; pos; pos = pos->next) #define hlist_for_each_safe(pos, n, head) \ - for (pos = (head)->first; pos && ({ n = pos->next; true; }); pos = n) + for (pos = (head)->first; pos && ({ n = pos->next; true; }); pos = n) -int * -twosum(int numbers[], int n, int target) +static int * twosum(int *nums, int numsSize, int target) { - int i, j; - struct hash_table - { - struct hlist_head h; - }; - struct plus_elem - { - int index; - int num; - struct hlist_node n; - }; - - struct hash_table *ht = malloc(n * sizeof(*ht)); - if (ht == NULL) { - return NULL; - } - for (i = 0; i < n; i++) { - INIT_HLIST_HEAD(&ht[i].h); - } - struct plus_elem *elems = malloc(n * sizeof(*elems)); - if (elems == NULL) { - return NULL; - } - for (i = 0; i < n; i++) { - elems[i].num = 0; - INIT_HLIST_NODE(&elems[i].n); + struct hash_table { + struct hlist_head head; + }; + struct plus_elem { + int index; + int num; + struct hlist_node node; + }; + + int i, j; + struct hash_table *ht = malloc(numsSize * sizeof(*ht)); + for (i = 0; i < numsSize; i++) { + INIT_HLIST_HEAD(&ht[i].head); + } + + struct plus_elem *elems = malloc(numsSize * sizeof(*elems)); + for (i = 0; i < numsSize; i++) { + elems[i].num = 0; + INIT_HLIST_NODE(&elems[i].node); + } + + for (i = 0; i < numsSize; i++) { + int num = nums[i] < 0 ? -nums[i] : nums[i]; + int hash = num % numsSize; + elems[i].index = i; + elems[i].num = nums[i]; + hlist_add_head(&elems[i].node, &ht[hash].head); + } + + for (i = 0; i < numsSize; i++) { + int num = target - nums[i]; + int h1 = nums[i] < 0 ? -nums[i] % numsSize : nums[i] % numsSize; + int h2 = num < 0 ? -num % numsSize : num % numsSize; + int i1 = -1, i2 = -1; + struct hlist_node *pos; + hlist_for_each(pos, &ht[h1].head) { + struct plus_elem *elem = hlist_entry(pos, struct plus_elem, node); + if (elem->num == nums[i]) { + i1 = elem->index; + break; + } } - - for (i = 0; i < n; i++) { - int num = numbers[i] < 0 ? -numbers[i] : numbers[i]; - int hash = num % n; - elems[i].index = i + 1; - elems[i].num = numbers[i]; - hlist_add_head(&elems[i].n, &ht[hash].h); + hlist_for_each(pos, &ht[h2].head) { + struct plus_elem *elem = hlist_entry(pos, struct plus_elem, node); + if (elem->num == num) { + if (i1 != elem->index) { + i2 = elem->index; + break; + } + } } - for (i = 0; i < n; i++) { - int num = target - numbers[i]; - int h1 = numbers[i] < 0 ? -numbers[i] % n : numbers[i] % n; - int h2 = num < 0 ? -num % n : num % n; - int i1 = 0, i2 = 0; - struct hlist_node *pos; - hlist_for_each(pos, &ht[h1].h) { - struct plus_elem *elem = hlist_entry(pos, struct plus_elem, n); - if (elem->num == numbers[i]) { - i1 = elem->index; - break; - } - } - hlist_for_each(pos, &ht[h2].h) { - struct plus_elem *elem = hlist_entry(pos, struct plus_elem, n); - if (elem->num == num) { - if (i1 != elem->index) { - i2 = elem->index; - break; - } - } - } - if (i1 > 0 && i2 > 0) { - int *indexes = malloc(2 * sizeof(int)); - if (indexes == NULL) { - break; - } - indexes[0] = i1 < i2 ? i1 : i2; - indexes[1] = i1 > i2 ? i1 : i2; - return indexes; - } + if (i1 >= 0 && i2 >= 0) { + int *indexes = malloc(2 * sizeof(int)); + if (indexes == NULL) { + break; + } + indexes[0] = i1 < i2 ? i1 : i2; + indexes[1] = i1 > i2 ? i1 : i2; + return indexes; } + } - return NULL; + return NULL; } -int -main(void) +int main(void) { - //int numbers[] = {-1, -2, -3, -4, -5}; - //int target = -8; - int numbers[] = {0,4,3,0}; - int target = 0; - int *indexes = twosum(numbers, sizeof(numbers) / sizeof(numbers[0]), target); - if (indexes != NULL) { - printf("%d %d\n", indexes[0], indexes[1]); - } else { - printf("Not found\n"); - } - - return 0; + //int nums[] = {-1, -2, -3, -4, -5}; + //int target = -8; + //int nums[] = {0,4,3,0}; + //int target = 0; + int nums[] = { 3, 2, 4 }; + int target = 6; + int *indexes = twosum(nums, sizeof(nums) / sizeof(nums[0]), target); + if (indexes != NULL) { + printf("%d %d\n", indexes[0], indexes[1]); + } else { + printf("Not found\n"); + } + + return 0; }