力扣刷题记录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;
}
相关推荐
那个村的李富贵20 小时前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
power 雀儿21 小时前
Scaled Dot-Product Attention 分数计算 C++
算法
琹箐21 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
renhongxia11 天前
如何基于知识图谱进行故障原因、事故原因推理,需要用到哪些算法
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·知识图谱
坚持就完事了1 天前
数据结构之树(Java实现)
java·算法
算法备案代理1 天前
大模型备案与算法备案,企业该如何选择?
人工智能·算法·大模型·算法备案
赛姐在努力.1 天前
【拓扑排序】-- 算法原理讲解,及实现拓扑排序,附赠热门例题
java·算法·图论
野犬寒鸦1 天前
从零起步学习并发编程 || 第六章:ReentrantLock与synchronized 的辨析及运用
java·服务器·数据库·后端·学习·算法
霖霖总总1 天前
[小技巧66]当自增主键耗尽:MySQL 主键溢出问题深度解析与雪花算法替代方案
mysql·算法
rainbow68891 天前
深入解析C++STL:map与set底层奥秘
java·数据结构·算法