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

18、最大子数组和

一开始写错了(对和进行了跨越相加,而不是抛弃重新开始)

cpp 复制代码
int maxSubArray(int* nums, int numsSize) 
{
    int sum = 0;//当前值
    int if_have_sum = 0;//如果有的话的值
    sum = nums[0];
    if_have_sum = sum;
    for(int i =1; i < numsSize; i++)
    {
        if(nums[i] >= sum)//且大于当前最大和
        {
            if(if_have_sum > 0)
            {
                sum = if_have_sum + nums[i];
                if_have_sum = sum;
            }
            else
            {
                sum = nums[i];
                if_have_sum = sum;
            }

        }
        else if(sum <= nums[i]+if_have_sum)//和比之前加了负值大
        {
            sum = nums[i]+if_have_sum;
            if_have_sum = sum;
        }
        else
        {
            if_have_sum = nums[i] + if_have_sum;
        }
    }
    return sum;
}

后面加了一个globle参数进行历史记录,后边进行重新开始合计

cpp 复制代码
int maxSubArray(int* nums, int numsSize) 
{
    int curren_sum = nums[0];
    int globle_sum = curren_sum;
    for(int i = 1; i < numsSize; i++)
    {
        curren_sum = (nums[i] > (curren_sum + nums[i])) ? nums[i] : curren_sum + nums[i];//注意需要更新和,不能直接跳过
        globle_sum = (curren_sum > globle_sum) ? curren_sum : globle_sum;
    }
    return globle_sum;
}

19、环形链表二(142链表 双指针)

注意while(Fast->next != NULL && Fast != NULL) 判断顺序写反

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) 
{
    if(head == NULL)return NULL;
    struct ListNode *Fast = head;
    struct ListNode *Slow = head;

    while(Fast != NULL && Fast->next != NULL )
    {
        Fast = Fast->next ->next;
        Slow = Slow -> next;
        if(Fast == Slow)
        {
            Slow = head;
            while(Fast != Slow)
            {
                Slow = Slow -> next;
                Fast = Fast -> next;
            }
            return Slow;
        }
    }
    return NULL;
}

20、合并两个有序链表(21链表)

每次只比较最小值,使用一个中间变量记录头

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) 
{
    struct ListNode temp;
    struct ListNode* head = &temp;
    temp.next = NULL;

    while(list1 != NULL && list2 != NULL)
    {
        if(list1->val <= list2->val)
        {
            head -> next = list1;
            list1 = list1 -> next;
        }
        else
        {
            head -> next = list2;
            list2 = list2 -> next;
        }
        head = head -> next;
    }
    if(list1 == NULL)
    {
        head -> next = list2;
    }
    else
    {
        head -> next = list1;
    }
    return temp.next;
}

21、不同路径(62动态规划阶乘)

注意要边乘边除以,不然溢出很难搞(longlong),还要注意除时的分数问题(float),

cpp 复制代码
int uniquePaths(int m, int n) 
{
    long long result = 1;
    if(m > n)
    {
        int temp = m;
        m = n;
        n = temp;
    }
    for(int i = n; i< m+n -1 ;i++)
    {
        result *= i;
        result /= (i-n+1);
    }
    return (int)result;
}

22、二叉树直径(543递归)

递归到底层 每次+1 对比左右深度取max(需要初始化max = 0)

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int max = 0;

int get_deepth(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }
    int leftdeep = get_deepth(root -> left);
    int rightdeep = get_deepth(root -> right);
    max = max > leftdeep + rightdeep ? max : leftdeep + rightdeep;
    leftdeep = leftdeep>rightdeep?leftdeep:rightdeep;
    return leftdeep + 1;
}


int diameterOfBinaryTree(struct TreeNode* root) 
{
    max = 0;
    get_deepth(root);
    return max;
}

23、颜色分类(75技巧 三指针)

注意[2] [2,1,2]单个数值和首尾相同的情况

cpp 复制代码
void sortColors(int* nums, int numsSize) 
{
    int pZero = 0;
    int pRemove = 0;
    int pTwo = numsSize - 1;
    if(numsSize == 1)return;
    for(; pRemove<numsSize; pRemove++)
    {
        if(nums[pRemove] == 2)
        {
            while(nums[pTwo] == 2)
            {
                pTwo--;
                if(pTwo < pRemove)return;
            }
            int temp = nums[pTwo];
            nums[pTwo] = 2;
            pTwo--;
            nums[pRemove] = temp;
        }
        if(nums[pRemove] == 0)
        {
            int temp = nums[pZero];
            nums[pZero] = 0;
            pZero++;
            nums[pRemove] = temp;
        }
        if(pTwo <= pRemove)return;
    }
}

24、寻找重复数(287 技巧 我用的哈希表)

cpp 复制代码
int findDuplicate(int* nums, int numsSize) 
{
    int *hashmap = (int *)malloc(sizeof(int)*(numsSize));
    int result = -1;
    memset(hashmap,-1,sizeof(int)*(numsSize));
    for(int i = 0; i<numsSize;i++)
    {
        if(hashmap[nums[i]] != -1)
        {
            result = nums[i];
            break;
        }
        hashmap[nums[i]] = 1;
    }
    return result;
}    

24、买股票的最佳时机(121 贪心)

cpp 复制代码
int maxProfit(int* prices, int pricesSize) 
{
    int pLow = 0;
    int pHigt = 0;
    int max = 0;
    for(int i = 0;i<pricesSize;i++)
    {
        if(prices[i] - prices[pLow] > max)
        {
            max = prices[i] - prices[pLow];
            pHigt = i;
        }
        else if(prices[i] < prices[pLow])
        {
            pLow = i;
        }
    }    
    return max;
}
相关推荐
dazzle2 小时前
Python数据结构(十五):归并排序详解
数据结构·python·算法
启友玩AI2 小时前
方言守护者:基于启英泰伦CI-F162GS02J芯片的“能听懂乡音”的智能夜灯DIY全攻略
c语言·人工智能·嵌入式硬件·ai·语音识别·pcb工艺
2301_764441332 小时前
基于paCy模型与jsoncrack进行依存句法分析
python·算法·自然语言处理
EmbedLinX2 小时前
Linux 之设备驱动
linux·服务器·c语言
咩咩不吃草2 小时前
【逻辑回归】:从模型训练到评价
算法·机器学习·逻辑回归
ersaijun2 小时前
机器人运动控制关键算法体系:从理论框架到前沿实践
算法·机器人
smj2302_796826523 小时前
解决leetcode第3826题.最小分割分数问题
数据结构·python·算法·leetcode
多多*3 小时前
2026年最新 测试开发工程师相关 Linux相关知识点
java·开发语言·javascript·算法·spring·java-ee·maven