力扣刷题记录6(无算法背景,纯C语言)

过年了,好几天没刷了

31、除了自身以外数组的乘积(238 前缀和 数组)

cpp 复制代码
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* productExceptSelf(int* nums, int numsSize, int* returnSize) 
{
    int *res = (int *)malloc(sizeof(int)*numsSize);
    returnSize[0] = numsSize;

    res[0] = 1;
    for(int i = 1; i < numsSize; i++)
    {
        res[i] = res[i - 1] * nums[i - 1];
    }
    int temp = 1;
    for(int i = numsSize - 1; i >= 0; i--)
    {
        res[i] *= temp;
        temp *=  nums[i];
    }
    return res;
}

32、岛屿数量(200 图论)

cpp 复制代码
void waterFlood(char** grid, int i, int j, int gridSize, int* gridColSize)
{
    if(i >= gridSize || i < 0  || j >= gridColSize[i] || j < 0)
    {
        return;
    }
    if(grid[i][j] != '1')
    {
        return;
    }
    grid[i][j] = '0';
    waterFlood(grid, i-1, j, gridSize, gridColSize);
    waterFlood(grid, i+1, j, gridSize, gridColSize);
    waterFlood(grid, i, j-1, gridSize, gridColSize);
    waterFlood(grid, i, j+1, gridSize, gridColSize);
}


int numIslands(char** grid, int gridSize, int* gridColSize) 
{
    int count = 0;
    for(int i = 0; i<gridSize; i++)
    {
        for(int j = 0; j < gridColSize[i]; j++)
        {
            if(grid[i][j] == '1')
            {
                count++;
                waterFlood(grid, i, j, gridSize, gridColSize);
            }
        }
    }    
    return count;
}

33、两数相加(2链表)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) 
{
    struct ListNode* temp = (struct ListNode*)malloc(sizeof(struct ListNode));
    temp -> next= NULL;
    temp -> val = 0;
    struct ListNode* l3 = temp;
    int flag = 0;
    while(l1 != NULL || l2 != NULL || flag != 0)
    {
        int val1 = l1 != NULL ? l1 -> val : 0;
        int val2 = l2 != NULL ? l2 -> val : 0;
        int sum = val1 + val2 + flag;
        flag = sum / 10;
        l3 -> next = (struct ListNode*)malloc(sizeof(struct ListNode));
        l3 -> next -> val = sum % 10;
        l3 -> next -> next = NULL;   
        if(l1 != NULL)l1 = l1 -> next;
        if(l2 != NULL)l2 = l2 -> next;
        l3 = l3 -> next;
    }
    struct ListNode* result = temp->next;
    return result;
}

34、删除链表倒数第N个节点(19链表)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) 
{
    
    int count = 1;
    struct ListNode* result = head;
    struct ListNode* temp = (struct ListNode *)malloc(sizeof(struct ListNode));
    temp -> val = 0;
    temp -> next = head;
    while(head -> next != NULL)
    {
        head = head -> next;
        count++;
        if(count > n)
        {
            temp = temp -> next;
        }
    }
    //head被删除的情况,返回原来表头的下一位
    if(count == n)
    {
        return result -> next;
    }
    //链表最后一个元素的情况
    if(temp -> next == NULL || temp -> next -> next == NULL) 
    {
        temp -> next = NULL;
    }
    else 
    {
        temp -> next = temp -> next -> next;
    }
    //正常返回原来表头
    return result;
}

35、两两交换链表中的节点(24 链表递归)

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

void swap(struct ListNode* head)
{
    if(head != NULL && head -> next != NULL)
    {
        struct ListNode* headn = head -> next;
        if(headn -> next != NULL)
        {
            struct ListNode* headnn = headn -> next;
            if(headnn -> next != NULL)
            {
                struct ListNode* headnnn = headnn -> next;
                headn -> next = head;
                head -> next = headnnn;
                swap(headnn);
                return;
            }
            headn -> next = head;
            head -> next = headnn;
            return;
        }
        headn -> next = head;
        head -> next = NULL;
        return;
    }
    return;
}

struct ListNode* swapPairs(struct ListNode* head) 
{
    if(head != NULL && head -> next != NULL)
    {
        struct ListNode* headn = head -> next;
        swap(head);
        return headn;
    }
    return head;
}

简洁解

cpp 复制代码
struct ListNode* swapPairs(struct ListNode* head) {
    // 1. 终止条件:如果没有节点或只有一个节点,无需交换
    if (head == NULL || head->next == NULL) {
        return head;
    }

    // 2. 设定要交换的两个节点
    struct ListNode* firstNode = head;
    struct ListNode* secondNode = head->next;

    // 3. 递归处理后续链表,并连接到当前 pair 的末尾
    // firstNode 交换后会变成第二个,所以它的 next 指向后面交换完的结果
    firstNode->next = swapPairs(secondNode->next);

    // 4. 完成当前的交换
    secondNode->next = firstNode;

    // 5. 返回交换后的新头节点(即原本的第二个节点)
    return secondNode;
}

36、二叉树层序遍历(102 队列)

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * 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** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) 
{
    *returnSize = 0;
    if(root == NULL)return NULL;
    int** result = (int**) malloc(sizeof(int*)*2000);
    *returnColumnSizes = (int*) malloc(sizeof(int)*2000);
    int head = 0;
    int tail = 0;
    struct TreeNode *queue[2000];
    queue[head++] = root;
    while(head > tail)
    {
        int levelSize = head - tail;
        result[*returnSize] = (int*) malloc(sizeof(int)*levelSize);
        (*returnColumnSizes)[*returnSize] = levelSize;
        for(int i = 0; i < levelSize; i++)
        {
            (result)[*returnSize][i] = queue[tail] -> val;
            if(queue[tail] ->  left)queue[head++] = queue[tail] ->  left;
            if(queue[tail] ->  right)queue[head++] = queue[tail] ->  right;
            tail++;
        }
        (*returnSize)++;
    }
    return result;
}
相关推荐
-To be number.wan2 小时前
算法学习日记 | 双指针
c++·学习·算法
爱编码的小八嘎2 小时前
第2章 认识CPU-2.3 32位微处理器(1)
c语言
样例过了就是过了2 小时前
LeetCode热题100 最大子数组和
数据结构·算法·leetcode
BackCatK Chen2 小时前
第十五章 吃透C语言结构与数据形式:struct/union/typedef全解析
c语言·开发语言·数据结构·typedef·结构体·函数指针·联合体
铸人2 小时前
再论自然数全加和 - 欧拉伽马常数
数学·算法·数论·复数
『往事』&白驹过隙;2 小时前
C/C++中的格式化输出与输入snprintf&sscanf
linux·c语言·c++·笔记·学习·iot·系统调用
踩坑记录3 小时前
leetcode hot100 200. 岛屿数量 medium dfs
leetcode·深度优先
m0_531237173 小时前
C语言-初始化赋值,函数,变量的作用域与生命周期
c语言·开发语言
m0_531237173 小时前
C语言-变量,枚举常量,字符串,打印类型,转义字符
c语言·数据结构·算法