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

1、零移动(238双指针)

使用一个快指针一个慢指针,一个用来填充一个用来查找,最后补零

cpp 复制代码
void moveZeroes(int* nums, int numsSize) 
{
    int slow = 0;
    for(int fast = 0; fast < numsSize; fast ++)
    {
        if(nums[fast] != 0)
        {
            nums[slow++] = nums[fast];
        }
    }
    for(int fast = slow; fast < numsSize; fast ++)
    {
        nums[fast] = 0;
    }
}

2、翻转链表(206链表)

cpp 复制代码
struct ListNode* reverseList(struct ListNode* head) 
{
    struct ListNode *temp1 = NULL;
    struct ListNode *temp2 = head;
    while(temp2 != NULL)
    {
        struct ListNode *temp3 = temp2 -> next;
        temp2 -> next = temp1;
        temp1 = temp2;
        temp2 = temp3;
    }
    return temp1;
}

3、相交链表(160 双指针链表)

cpp 复制代码
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *pA = headA;
    struct ListNode *pB = headB;
    int Flag = 0;
    while(pA != pB)
    {
        if(pB->next == NULL)
        {
            pB = headA;
            Flag++;
        }
        else
        {
            pB = pB->next;
        }
        if(pA->next == NULL)
        {
            pA = headB;
            Flag++;
        }
        else
        {
            pA = pA->next;
        }
        if(Flag > 2)return NULL;
    }
    return pA;
}

4、回文链表(234链表双指针)

cpp 复制代码
bool isPalindrome(struct ListNode* head) {
    //找中点(Fast走两步,Slow走一步)
    if(head == NULL || head -> next == NULL) return true;
    struct ListNode* pSlow = head;
    struct ListNode* pFast = head;
    
    while(pFast != NULL && pSlow != NULL)
    {
        pFast = pFast -> next;
        //判断奇偶
        if(pFast == NULL)
        {
            pSlow -> next;
            break;
        }
        pFast = pFast -> next;
        pSlow = pSlow -> next;
    }
    //翻转后边的链表
    struct ListNode* temp1 = NULL;
    struct ListNode* temp2 = pSlow;
    while(temp2 != NULL)
    {
        struct ListNode* temp3 = temp2->next;
        temp2 -> next = temp1;
        temp1 = temp2;
        temp2 = temp3;
    }
    //比较
    pFast = head;
    pSlow = temp1;
    while(pSlow != NULL)
    {
        if(pFast -> val != pSlow -> val) return false;
        pFast = pFast->next;
        pSlow = pSlow->next;
    }
    return true;
}   

5、环形列表(141链表双指针)

cpp 复制代码
bool hasCycle(struct ListNode *head) 
{
    if (head == NULL || head->next == NULL) return false;
    struct ListNode *pFast = head->next;
    struct ListNode *pSlow = head;

    while(pFast != pSlow)
    {
        if(pFast == NULL || pFast -> next == NULL)return false;
        pFast = pFast -> next -> next;
        pSlow = pSlow -> next;
    }
    return true;
}

6、盛最多水的容器(11数组双指针)

cpp 复制代码
int maxArea(int* height, int heightSize) {
    int pPre = 0;
    int pBac = heightSize-1;
    int maxArea = height[pPre] > height[pBac] ? height[pBac] * abs(pBac - pPre) : height[pPre] * abs(pBac - pPre);
    int newArea = 0;
    while(pBac > pPre)
    {
        //如果前面长后边短,后面就要找一根比前面长的
        if(height[pPre] >= height[pBac])
        {
            int prehight = height[pBac];
            while((prehight >= height[pBac]) && (pBac > pPre))
            {
                pBac--;
            }
        }
        newArea = height[pPre] > height[pBac] ? height[pBac] * abs(pBac - pPre) : height[pPre] * abs(pBac - pPre);
        maxArea = maxArea > newArea ? maxArea : newArea;
        //反之
        if(height[pPre] < height[pBac])
        {
            int prehight = height[pPre];
            while((prehight >= height[pPre]) && (pBac > pPre))
            {
                pPre++;
            }
        }
        newArea = height[pPre] > height[pBac] ? height[pBac] * abs(pBac - pPre) : height[pPre] * abs(pBac - pPre);
        maxArea = maxArea > newArea ? maxArea : newArea;
    }
    return maxArea;
}
相关推荐
Gorway5 分钟前
解析残差网络 (ResNet)
算法
拖拉斯旋风16 分钟前
LeetCode 经典算法题解析:优先队列与广度优先搜索的巧妙应用
算法
Wect18 分钟前
LeetCode 207. 课程表:两种解法(BFS+DFS)详细解析
前端·算法·typescript
灵感__idea14 小时前
Hello 算法:众里寻她千“百度”
前端·javascript·算法
Wect1 天前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
NAGNIP1 天前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
颜酱2 天前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub2 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub2 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub2 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉