leetcode题目分类 & 常用代码

题目分类

1、DFS+BFS

https://leetcode-cn.com/problems/number-of-provinces/
https://leetcode-cn.com/problems/keys-and-rooms/
https://leetcode-cn.com/problems/deepest-leaves-sum/
https://leetcode-cn.com/problems/binary-tree-right-side-view
https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/

2、递归+回溯

https://leetcode-cn.com/problems/jump-game-iii/
https://leetcode-cn.com/problems/permutations/
https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/
https://leetcode-cn.com/problems/bracket-lcci/
https://leetcode-cn.com/problems/split-a-string-into-the-max-number-of-unique-substrings/

常用代码

Uthash

/* 定义节点结构 */
struct NODE {
    int key;
    int val;
    UT_hash_handle hh;
};
/* 定义hash表 */
struct NODE *table = NULL;

/* 查找 */
struct NODE * Find(int key)
{
    struct NODE *t;
    HASH_FIND_INT(table, &key, t);
    return t;
}

/* 添加元素,需要先判断是否存在 */
void Add(int key, int val)
{
    struct NODE *t = Find(key);
    if (t == NULL) {
        t = (struct NODE *)malloc(sizeof(struct NODE));
        t->key = key;
        t->val = val;
        HASH_ADD_INT(table, key, t);
    } else {
        /* 操作节点 */
    }
}

/* 删除 */
void Delete(struct NODE *t) {
    HASH_DEL(table,t);
    free(t);
    t = NULL;
}

/* 删除所有节点 */
void DeleteAll() {
  struct NODE *t, *tmp;
  /* 遍历 */
  HASH_ITER(hh, table, t, tmp) {
    HASH_DEL(table,t);
    free(t);
  }
}

/* 统计 */
int cnt = HASH_COUNT(table);

递归 + 回溯

典型例题:
https://leetcode-cn.com/problems/jump-game-iii/
https://leetcode-cn.com/problems/permutations/

递归常见代码

void GetRet(cur) {
    /* 终止条件 */
    if (finish) {
        return;
    }
    
    /* 当前问题依赖于子问题的结果集的"和" */
    ret = sum(GetRet(cur.son[i]));
}

回溯常见代码

void Dfs(cur) {
    /* 终止条件 */
    if (finish) {
        /* 保存结果 */
        save();
        return;
    }

    /* 状态置位 */
    Tag(cur);

    Dfs(cur.next);

    /* 状态重置 */
    UnTag(cur);
}

并查集

典型例题:
https://leetcode-cn.com/problems/number-of-provinces/
https://leetcode-cn.com/problems/most-stones-removed-with-same-row-or-column/

并查集常见代码
用数组 模拟 树结构

    /* 初始化,所有节点指向自己 */
    v[i] = i;

int Find(int *v, int x)
{
    /* 寻找root点 */
    int k = x;
    while(v[k] != k) {
        k = v[k];
    }
    /* 路径压缩 */
    int t = x;
    while (v[t] != t) {
        int m = v[t];
        v[t] = k;
        t = m;
    }
    return k;
}

void Union(int *v, int x, int y)
{
    int a = Find(v, x);
    int b = Find(v, y);
    /* 按需合并,不一定是大小关系 */
    if (a > b) {
        a ^= b;
        b ^= a;
        a ^= b;
    }
    /* 归并为一组 */
    v[b] = a;

DFS、BFS

典型题目
https://leetcode-cn.com/problems/number-of-provinces/
https://leetcode-cn.com/problems/keys-and-rooms/

DFS常见代码结构
递归实现,初始调用递归函数DFS(root)

void Dfs(current_node) {
    /* 终止条件 */
    if (finish) {
        return;
    }
    /* 下个节点 */
    Dfs(next_node);
}

BFS常见结构
队列实现,控制下标head、last(指针也可以),队列h

void Dfs(current_node) {
    /* 终止条件 */
    if (finish) {
        return;
    }
    /* 下个节点 */
    Dfs(next_node);
}

hash + 滑动窗口

典型题目:
https://leetcode-cn.com/problems/max-number-of-k-sum-pairs/
https://leetcode-cn.com/problems/4sum-ii/
https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/
https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/
https://leetcode-cn.com/problems/max-consecutive-ones-iii/
https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

部门集训
1、字符串(第一期 2021-6-7)
https://leetcode-cn.com/problems/zigzag-conversion/

https://leetcode-cn.com/problems/generate-parentheses/

https://leetcode-cn.com/problems/last-substring-in-lexicographical-order/

2、线性表(第二期 2021-6-14)
https://leetcode-cn.com/problems/maximum-sum-circular-subarray/

https://leetcode-cn.com/problems/invalid-transactions/

https://leetcode-cn.com/problems/maximal-rectangle/

3、队列(第三期 2021-6-21)
https://leetcode-cn.com/problems/design-circular-queue/

https://leetcode-cn.com/problems/task-scheduler/

https://leetcode-cn.com/problems/max-sum-of-rectangle-no-larger-than-k/

4、栈(第四期 2021-6-28)
https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses/

https://leetcode-cn.com/problems/valid-parenthesis-string/

https://leetcode-cn.com/problems/tag-validator/

5、链表(第五期 2021-7-5)
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/

https://leetcode-cn.com/problems/add-two-numbers/

https://leetcode-cn.com/problems/merge-k-sorted-lists/

6、哈希表(第六期 2021-7-12)
https://leetcode-cn.com/problems/brick-wall/

https://leetcode-cn.com/problems/fraction-to-recurring-decimal/

https://leetcode-cn.com/problems/longest-duplicate-substring/

7、二叉查找树(第七期 2021-7-19)
https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/

https://leetcode-cn.com/problems/top-k-frequent-words/

https://leetcode-cn.com/problems/sum-of-distances-in-tree/

8、堆(第八期 2021-7-26)
https://leetcode-cn.com/problems/sort-characters-by-frequency/

https://leetcode-cn.com/problems/reorganize-string/

https://leetcode-cn.com/problems/minimum-cost-to-hire-k-workers/

9、图论(第九期 2021-8-2)
https://leetcode-cn.com/problems/as-far-from-land-as-possible/

https://leetcode-cn.com/problems/shortest-path-with-alternating-colors/

https://leetcode-cn.com/problems/sort-items-by-groups-respecting-dependencies/

10、排序算法(第十期 2021-8-9)
https://leetcode-cn.com/problems/sort-colors/

https://leetcode-cn.com/problems/merge-intervals/

https://leetcode-cn.com/problems/insert-interval/

11、迭代、递归(第十一期 2021-8-16)
https://leetcode-cn.com/problems/valid-tic-tac-toe-state/

https://leetcode-cn.com/problems/k-th-symbol-in-grammar/

https://leetcode-cn.com/problems/number-of-atoms/

12、分治(第十二期 2021-8-23)
https://leetcode-cn.com/problems/search-a-2d-matrix/

https://leetcode-cn.com/problems/powx-n/

https://leetcode-cn.com/problems/online-majority-element-in-subarray/

13、搜索算法(第十三期 2021-8-30)
https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/

https://leetcode-cn.com/problems/flatten-a-multilevel-doubly-linked-list/

https://leetcode-cn.com/problems/clone-graph/

14、贪心(第十四期 2021-9-6)
https://leetcode-cn.com/problems/jump-game/

https://leetcode-cn.com/problems/gas-station/

https://leetcode-cn.com/problems/jump-game-ii/

15、动态规划(第十五期 2021-9-13)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值