力扣刷题记录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;
}
相关推荐
大鱼>2 小时前
多宠物家庭智能管理平台:云端架构与多设备协同实战
python·算法·iot·宠物
炸膛坦客2 小时前
单片机/C/C++八股:(二十四)编译文件( .bin 和 .hex ,包括 .elf 和 .axf )
c语言·c++·单片机
To_OC2 小时前
LC 22 括号生成:刷完这道题,我终于搞懂回溯剪枝了
javascript·算法·leetcode
To_OC2 小时前
LC 39 组合总和:回溯入门必刷题,我踩过的两个坑都在这了
javascript·算法·leetcode
数字杂技师2 小时前
每条推荐都很准,为什么我还是越刷越无聊?
算法
兰令水2 小时前
hot100【acm版】【2026.7.14打卡-java版本】
java·数据结构·算法·leetcode·面试
cpp_25013 小时前
P10098 [ROIR 2023] 地铁建设 (Day 2)
数据结构·c++·算法·贪心·二分答案·洛谷题解
阳明山水4 小时前
TimesFM与Moirai MoE零样本预测解析
人工智能·深度学习·算法·机器学习·架构
铅笔侠_小龙虾4 小时前
Rust 学习(2)-变量、常量与 shadowing
学习·算法·rust
AI科技星4 小时前
基于全域数学公理体系的三元极值题最简求解法【乖乖数学】
线性代数·算法·游戏·决策树·机器学习·乖乖数学·全域数学