zoukankan      html  css  js  c++  java
  • c语言刷lleetcode记录

    155. 最小栈

    笔记:

    在push(压栈)的时候思路:

     

    typedef struct MinStack{
        int data;
        struct MinStack *next;
    } MinStack;
    
    /** initialize your data structure here. */
    
    MinStack* minStackCreate() {
        MinStack *S = (MinStack*)malloc(sizeof(MinStack));  //为头指针分配空间
        S->data = 0;                                        //初始化
        S->next = NULL;
        return S;
    }
    
    void minStackPush(MinStack* obj, int val) {
        MinStack *s = (MinStack*)malloc(sizeof(MinStack));  //分配空间
        s->data = val;
        s->next = obj->next;
        obj->next = s;
    }
    
    void minStackPop(MinStack* obj) {
        MinStack *s;                                        //s为栈顶指针
        if(obj->next == NULL)                               //栈空
            return ;
        s = obj->next;
        obj->next = s->next;
        free(s);                                            //释放空间
    }
    
    int minStackTop(MinStack* obj) {
        return obj->next->data;
    }
    
    int minStackGetMin(MinStack* obj) {
        MinStack *p = obj->next;
        int min = p->data;                                  //初始化栈顶元素为最小值
        while(p != NULL){                                   //遍历栈
            if(p->data < min)                               //若遍历元素小于min则修改min为该元素
                min = p->data;
            p = p->next;
        }
        return min;
    }
    
    void minStackFree(MinStack* obj) {                      //释放空间
        MinStack *p = obj;
        MinStack *s;
        while(p != NULL){
            s = p->next;
            free(p);
            p = s;
        }
    }
    
    /**
     * Your MinStack struct will be instantiated and called as such:
     * MinStack* obj = minStackCreate();
     * minStackPush(obj, val);
     
     * minStackPop(obj);
     
     * int param_3 = minStackTop(obj);
     
     * int param_4 = minStackGetMin(obj);
     
     * minStackFree(obj);
    */

    二叉树的广度优先遍历(BFS)和层序遍历相关的题目:

    剑指 Offer 32 - I. 从上到下打印二叉树

    解题链接

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    
    
    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* levelOrder(struct TreeNode* root, int* returnSize)
    {
        // 模拟队列
        struct TreeNode *queue[1100];
        int front = -1;
        int rear = -1;
        int len = 0; // 用来记录数组的长度
        struct TreeNode *p; // 工作指针
        // 待返回的结果数组
        int *array = (int *)malloc(sizeof(int) * 1100);
    
        if (root == NULL) {
            *returnSize = 0;
            return root;
        }
        // 根节点先入列
        queue[++rear] = root;
        while (front < rear) {
            // 队列头元素出队
            p = queue[++front];
            // 结果数组存入节点的val
            array[len++] = p->val;
            // 如果当前节点有左孩子,那就入队
            if (p->left != NULL) {
                queue[++rear] = p->left;
            }
            // 如果当前节点有右孩子,那就入队
            if (p->right != NULL) {
                queue[++rear] = p->right;
            }
        }
        *returnSize = len;
        return array;
    }

    102. 二叉树的层序遍历

    /**
     * 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)
    {
        
        struct TreeNode *queue[10000];
        int front = 0;
        int rear = 0;
        // 工作指针
        struct TreeNode *p = NULL;
        // 结果数组
        int **resArray = (int **)malloc(sizeof(int *) * 10000);
        *returnColumnSizes = (int *)malloc(sizeof(int) * 10000);
        *returnSize = 0;
    
        if (root == NULL) {
            *returnSize = 0;
            **returnColumnSizes = 0;
            return resArray;
        }
        
        // 先取到头结点,入队
        queue[rear++] = root;
        // 大循环
        while (front != rear) {
            int temp = rear;
            int k = 0;
            resArray[*returnSize] = (int *)malloc(sizeof(int) * (rear - front));
            /* 小循环 
               为什么要用temp是因为rear的值会变(rear指向队列尾节点)
            */
            while (front < temp) {
                p = queue[front++];
                resArray[*returnSize][k++] = p->val;
                if (p->left) {
                    queue[rear++] = p->left;
                }
                if (p->right) {
                    queue[rear++] = p->right;
                }
            }
            (*returnColumnSizes)[*returnSize] = k;
            (*returnSize)++;
        }
    
        return resArray;
    }

     链接

    (最短)路径相关的题目:

    112. 路径总和

    112题要背下来!!!

    回溯法:

    46

    47

  • 相关阅读:
    Cousera课程Learning How to Learn学习报告
    C语言中当无符号数遇到符号数
    STC15 串口(工作方式1)使用小结
    取C语言头文件的文件名
    linux 的 shell 逻辑
    Win7 局域网内简单共享的设置
    写了一个批处理,可以实现文件备份,自动对比删除冗余文件。
    C语言 函数指针的应用
    自动控制原理 典型环节频率特性对比
    51单片机汇编的溢出标志位OV和进位标志位CY
  • 原文地址:https://www.cnblogs.com/kongweisi/p/15336732.html
Copyright © 2011-2022 走看看