力扣刷题记录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;
}
相关推荐
CoovallyAIHub13 小时前
181小时视频丢给GPT-5,准确率只有15%——南大联合NVIDIA等五校发布多模态终身理解数据集
深度学习·算法·计算机视觉
CoovallyAIHub14 小时前
CVPR 2026 | GS-CLIP:3D几何先验+双流视觉融合,零样本工业缺陷检测新SOTA,四大3D工业数据集全面领先!
深度学习·算法·计算机视觉
xlp666hub14 小时前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
有意义16 小时前
深度拆解分割等和子集:一维DP数组与倒序遍历的本质
前端·算法·面试
xlp666hub17 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
用户7268761033718 小时前
解放双手的健身助手:基于 Rokid AR 眼镜的运动计时应用
算法
Wect18 小时前
LeetCode 17. 电话号码的字母组合:回溯算法入门实战
前端·算法·typescript
xlp666hub1 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
ZhengEnCi2 天前
08c. 检索算法与策略-混合检索
后端·python·算法