1. 程式人生 > >LeetCode 15. 三數之和 3Sum(C語言)

LeetCode 15. 三數之和 3Sum(C語言)

題目描述:

給定一個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重複的三元組。
注意:答案中不可以包含重複的三元組。

例如, 給定陣列 nums = [-1, 0, 1, 2, -1, -4],
滿足要求的三元組集合為:
[
[-1, 0, 1],
[-1, -1, 2]
]

題目解答:

方法1:排序法

在兩數之和中,雙指標求目標數的方法可以用在這裡。外層遍歷陣列元素,將其相反數當作目標數,內層用雙指標法確定是否存在目標數。需要注意的是重複元素的過濾,儲存符合題意解之後,要跳過相等的數字,避免產生重複解。執行時間56ms左右,程式碼如下。

/**
 * Return an array of arrays of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int comp(void* a, void* b) {
    return *(int*)a > *(int*)b ? 1 : -1;
}
int** threeSum(int* nums, int numsSize, int* returnSize) {
    int n = numsSize;
    qsort(nums,
n, sizeof(int), comp); if(n < 3 || nums[0] > 0 || nums[0] + nums[1] + nums[2] > 0) return NULL; int** result = NULL; int i = 0, size = 0; int left = 0, right = 0; for(i = 0; i < n - 2 && nums[i] <= 0; i++) { left = i + 1; right = n - 1; while
(left < right) { if(nums[i] + nums[left] + nums[right] > 0) right--; else if(nums[i] + nums[left] + nums[right] < 0) left++; else { size++; result = (int**)realloc(result, size * sizeof(int*)); result[size - 1] = (int*)malloc(3 * sizeof(int)); result[size - 1][0] = nums[i]; result[size - 1][1] = nums[left]; result[size - 1][2] = nums[right]; while(left + 1 < n && nums[left] == nums[left + 1]) left++; while(right - 1 >= 0 && nums[right] == nums[right - 1]) right--; left++; right--; } } while(i + 1 < n && nums[i] == nums[i + 1]) i++; } *returnSize = size; return result; }

方法2: Hash+排序

排序完之後,將大於等於0的數字存入hash,直接申請最大正數max加1的空間,出現過的數字標記為真,否則標記為假。接下來利用兩層迴圈,在迴圈最內側,利用hash查詢符合條件的第三個數字是否存在,在找第三個數字時,符合以下判斷條件即可儲存:

  1. target <= max && hash[target] && nums[j] < target (最後一個條件保證儲存的結果都是遞增的,避免重複結果)
  2. target == nums[j] && flag (flag表示nums[j]之前是否有與其相等的數字,有則為真,無則為假)

另外需要跳過第一層迴圈中重複元素。執行時間48ms,程式碼如下。

/**
 * Return an array of arrays of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int comp(void* a, void* b) {
    return *(int*)a > *(int*)b ? 1 : -1;
}
int** threeSum(int* nums, int numsSize, int* returnSize) {
    int n = numsSize;
    int i = 0, j = 0, size = 0, t = 0;
    qsort(nums, n, sizeof(int), comp);
    if(n < 3 || nums[0] > 0 || nums[n - 1] < 0 || nums[0] + nums[1] + nums[2] > 0)
        return NULL;
    bool* hash = (bool*)calloc(nums[n - 1] + 1, sizeof(bool));
    for(i = 0; i < n; i++) { 
        if(nums[i] >= 0)
            hash[nums[i]] = true;
    }
    int** result = NULL;
    for(i = 0; i < n - 2 && nums[i] <= 0; i++) {
        for(j = i + 1; j < n - 1; j++) {
            bool flag = false;
            while(nums[j] == nums[j + 1]) {
                j++;
                flag = true;
            }  
            t = nums[i] + nums[j];
            if(t > 0)
                break;
            if((-t <= nums[n - 1] && hash[-t] && nums[j] < -t) || (-t == nums[j] && flag)) {
                size++;
                result = (int**)realloc(result, size * sizeof(int*));
                result[size - 1] = (int*)malloc(3 * sizeof(int));
                result[size - 1][0] = nums[i];
                result[size - 1][1] = nums[j];
                result[size - 1][2] = -t;
            }
        }
        while(nums[i] == nums[i + 1])
            i++;
    }
    *returnSize = size;
    return result;
}