From f61316c7f89c23bd4a5e83b7330d9ec605e13579 Mon Sep 17 00:00:00 2001 From: begeekmyfriend Date: Tue, 25 Jul 2017 15:27:01 +0800 Subject: [PATCH] Update Signed-off-by: begeekmyfriend --- 001_two_sum/two_sum.c | 3 ++- 015_three_sum/three_sum.c | 1 + 018_four_sum/four_sum.c | 6 +++--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/001_two_sum/two_sum.c b/001_two_sum/two_sum.c index e47986a..7dda376 100644 --- a/001_two_sum/two_sum.c +++ b/001_two_sum/two_sum.c @@ -101,7 +101,8 @@ static int * twosum(int *nums, int numsSize, int target) hlist_for_each(pos, &ht[hash].head) { struct plus_elem *elem = hlist_entry(pos, struct plus_elem, node); if (elem->num == other) { - if (elem->index != i) { + /* Eliminate duplicate */ + if (elem->index > i) { index = elem->index; break; } diff --git a/015_three_sum/three_sum.c b/015_three_sum/three_sum.c index 879fac5..e2e4ebc 100644 --- a/015_three_sum/three_sum.c +++ b/015_three_sum/three_sum.c @@ -123,6 +123,7 @@ static int** threeSum(int* nums, int numsSize, int* returnSize) { hlist_for_each(pos, &ht[hash].head) { struct plus_elem *elem = hlist_entry(pos, struct plus_elem, node); if (elem->num == other) { + /* Eliminate duplicate */ if (elem->index > j) { index = elem->index; break; diff --git a/018_four_sum/four_sum.c b/018_four_sum/four_sum.c index 8ec9e3a..77a3f98 100644 --- a/018_four_sum/four_sum.c +++ b/018_four_sum/four_sum.c @@ -133,8 +133,7 @@ static int** fourSum(int* nums, int numsSize, int target, int* returnSize) { for (i = 0; i < numsSize - 1; i++) { for (j = i + 1; j < numsSize; j++) { int new_target = target - nums[i] - nums[j]; - for (k = 0; k < numsSize; k++) { - if (k == i || k == j) continue; + for (k = j + 1; k < numsSize; k++) { int other = new_target - nums[k]; int hash = other < 0 ? -other % numsSize : other % numsSize; int index = -1; @@ -143,7 +142,8 @@ static int** fourSum(int* nums, int numsSize, int target, int* returnSize) { hlist_for_each(pos, &ht[hash].head) { struct element *elem = hlist_entry(pos, struct element, node); if (elem->num == other) { - if (elem->index != i && elem->index != j && elem->index != k) { + /* Eliminate duplicate */ + if (elem->index > k) { index = elem->index; break; }