leetcode训练(C语言) 016~020

16. 最接近的三数之和

leetcode训练(C语言) 016~020_第1张图片

int comp(const void* a, const void* b)
{
    return *(int*)a - *(int*)b;
}

int threeSumClosest(int* nums, int numsSize, int target)
{
    qsort(nums, numsSize, sizeof(int), comp);
    int sum = INT_MAX;
    int result = 0;
    for(int i = 0; i < numsSize - 2; i++) {
        int left = i + 1;
        int right = numsSize - 1;
        while(left < right) {
            result = nums[i] + nums[left] + nums[right] - target;
            if(result < 0) {
                left++;
            }
            else if(result == 0) {
                return target;
            }
            else {
                right--;
            }
            if(abs(result) < abs(sum)) {
                sum = result;
            }
        }
    }
    return  target + sum;
}

 

17. 电话号码的字母组合

leetcode训练(C语言) 016~020_第2张图片

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char ** letterCombinations(char * digits, int* returnSize)
{
    if(digits[0] == '\0') {
        *returnSize = 0; 
        return NULL;
    }
    char *dic[]= {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    int i, j, k;
    int len = strlen(digits);
    int size = 1;
    for(i = 0; digits[i] != '\0'; ++i) {
        size *= strlen(dic[digits[i] - 50]);
    }
    *returnSize = size;
    char** ans = (char**)malloc(size * sizeof(char*));
    for(i = 0; i < size; ++i) {
        ans[i] = (char *)malloc((len + 1) * sizeof(char));
    }
    int t = size, m, l;
    for(i = 0; i < len; ++i) {
        l = strlen(dic[digits[i] - 50]);
        t /= l;
        for(j = 0; j < l; ++j) {
            for(m = 0; m < size / t / l; ++m) {
                for(k = 0; k < t; ++k) {
                    ans[m * l * t + j * t + k][i] = dic[digits[i] - 50][j];
                }
            }     
        }
    }
    for(i = 0; i < size; ++i) {
        ans[i][len] = '\0';
    } 
    return ans;
}

 

18. 四数之和

leetcode训练(C语言) 016~020_第3张图片

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
 int compare(const void* a, const void* b) 
 {
    return (*(int*)a)-(*(int*)b);
}

int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** returnColumnSizes)
{
    int** returnArray = (int**)malloc(sizeof(int*) * 250);
    int ptr_ra = -1;
    *returnColumnSizes = (int *)malloc(sizeof(int) * 250);
    int i, j, k, t;
    int newTarget1, newTarget2;
    qsort(nums, numsSize, sizeof(int), compare);
    for (i = 0; i < numsSize - 3; i++) {
        if (i == 0 || ((i > 0) && (nums[i] != nums[i - 1]))) {
            newTarget1 = target - nums[i];
            for (j = i + 1; j < numsSize - 2; j++) {
                if (j == i + 1 || ((j > i + 1) && (nums[j] != nums[j - 1]))) {
                    k = j + 1;
                    newTarget2 = newTarget1 - nums[j];
                    t = numsSize - 1;
                    while (k < t) {
                        if (nums[k] + nums[t] == newTarget2) {
                            returnArray[++ptr_ra] = (int *)malloc(sizeof(int) * 4);
                            returnArray[ptr_ra][0] = nums[i];
                            returnArray[ptr_ra][1] = nums[j];
                            returnArray[ptr_ra][2] = nums[k];
                            returnArray[ptr_ra][3] = nums[t];
                            (*returnColumnSizes)[ptr_ra] = 4;
                            while (k < t && nums[k] == nums[k + 1]) k++;
                            while (k < t && nums[t] == nums[t - 1]) t--;
                            k++;
                            t--;
                        }
                        else if (nums[k] + nums[t] > newTarget2) {
                            while (k < t && nums[t] == nums[t - 1]) {
                                t--;
                            }
                            t--;
                        }
                        else {
                            while (k < t && nums[k] == nums[k + 1]) {
                                k++;
                            }
                            k++;
                        }
                    } 
               }
            }
        }
    }
    *returnSize = ptr_ra + 1;
    return returnArray;
}

 

19. 删除链表的倒数第N个节点

leetcode训练(C语言) 016~020_第4张图片

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* removeNthFromEnd(struct ListNode* head, int n)
{
    typedef struct ListNode list;
    list *p1 = head;
    int size = 0;
    while(p1 != NULL) {
        size++;
        p1 = p1->next;
    }
    if(size == 1) {   
        return NULL;
    }
    size = size - n;
    if(size == 0) {
        return head->next;
    }
    p1 = head;
    for(int i = 0; i < size - 1; i++) {
        p1 = p1->next;
    }
    p1->next = p1->next->next;
    return head;
}

 

20. 有效的括号

leetcode训练(C语言) 016~020_第5张图片leetcode训练(C语言) 016~020_第6张图片

bool isValid(char * s) 
{
    char a[10240];
    int i = 0;
    int j = -1;
    while (s[i] != '\0') {
        if (s[i] == '(') {
            a[++j] = '(';
        }
        else if (s[i] == '[') {
            a[++j] = '[';
        }
        else if (s[i] == '{') {
            a[++j] = '{';
        }
        else if (s[i] == ')') {
            if (j == -1 || a[j] != '(') {
                return false;
            }
            else {
                j--;
            }
        }
        else if (s[i] == ']') {
            if (j == -1 || a[j] != '[') {
                return false;
            }
            else {
                j--;
            }
        }
        else if (s[i] == '}') {
            if (j == -1 || a[j] != '{') {
                return false;
            }
            else {
                j--;
            }
        }
        i++;
    }
    if (j == -1) {
        return true;
    }
    else {
        return false;
    }
}

 

你可能感兴趣的:(leetcode训练(C语言) 016~020)