力扣刷题记录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;
}
相关推荐
Evand J2 分钟前
【课题推荐与代码介绍】卡尔曼滤波器正反向估计算法原理与MATLAB实现
开发语言·算法·matlab
DFT计算杂谈6 分钟前
VASP新手入门: IVDW 色散修正参数
linux·运维·服务器·python·算法
Chen_harmony21 分钟前
【习题02】打印菱形
c语言
吃着火锅x唱着歌22 分钟前
LeetCode 962.最大宽度坡
算法·leetcode·职场和发展
无限进步_32 分钟前
【C++】C++11的类功能增强与STL变化
java·前端·数据结构·c++·后端·算法
WL_Aurora38 分钟前
Python 算法基础篇之排序算法(一):冒泡、选择、插入
python·算法·排序算法
凌波粒40 分钟前
LeetCode--257. 二叉树的所有路径(二叉树)
算法·leetcode·职场和发展
AI算法沐枫41 分钟前
大一学生如何入门机器学习,深度学习,学习顺序如何?
人工智能·python·深度学习·学习·线性代数·算法·机器学习
codealy1 小时前
Rust 核心理论: 高并发与异步(三)
算法·rust
日月云棠1 小时前
JAVA数据结构与算法 - 基础:常用集合简述
java·算法