Leetcode 每个题型的模板题/例题总结

链表

lc14 环形链表 原题链接 难度:简单
通过set判断链表是否有环

括号和表达式相关

lc20 有效的括号 原题链接 难度:简单

使用栈处理,先入栈一个占位符?,然后遇到左括号入栈,遇到右括号出栈,并判断出栈的左括号和此时的右括号是不是同一类的。

Java版本参考代码

class Solution {
    public boolean isValid(String s) {
        HashMap<Character, Character> map = new HashMap<>();
        map.put('(', ')');
        map.put('[', ']');
        map.put('{', '}');
        map.put('?', '?');

        if (s.length() > 0 && !map.containsKey(s.charAt(0))) {
            // 说明以右括号开头,不合法的括号序列
            return false;
        }

        Deque<Character> stack = new LinkedList<>();
        stack.push('?');

        for (int i = 0; i < s.length(); i++) {
            Character cur = s.charAt(i);
            if (map.containsKey(cur)) {
                // 遇到左括号,入栈
                stack.push(cur);
            } else {
                // 当前字符是右括号
                char top = stack.pop();
                if (map.get(top) != cur) {
                    return false;
                }
            }
        }
        return stack.size() == 1;
    }
}

图论

lc785 判断二分图 原题链接 难度:中等

Java版本参考代码

class Solution {

    private int V;
    private int[] colors;
    private boolean[] visited;
    private int[][] graph;
    public boolean isBipartite(int[][] graph) {
        this.graph = graph;
        this.V = graph.length;
        this.colors = new int[V];
        this.visited = new boolean[V];
        for (int v = 0; v < V; v++) {
            if (!visited[v]) {
                if (!dfs(v, 0)) {
                    return false;
                }
            }
        }
        return true;
    }

    private boolean dfs(int v, int color) {
        visited[v] = true;
        colors[v] = color;

        for (int w : graph[v]) {
            if (!visited[w]) {
                // 如果没有访问过,则染另外一种颜色
                if (!dfs(w, 1 - color)) return false;
            } else {
                // 如果访问过,且颜色和当前节点v的颜色相同,则说明这个图不是二分图。
                if (colors[w] == colors[v]) {
                    return false;
                }
            }
        }
        return true;
    }
}

lc695 岛屿的最大面积 原题链接 难度:中等

方法一:自己建图

// Java版本参考代码
class Solution {

    private int R, C;
    private int[][] grid;
    private HashSet<Integer>[] G; // 建图,图的领接表表示
    private int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    private boolean[] visited;
    public int maxAreaOfIsland(int[][] grid) {
        if (grid == null) return 0;
        R = grid.length;
        if (R == 0) return 0;
        
        C = grid[0].length;
        if (C == 0) return 0;

        this.grid = grid;
        
        // 建图
        G = constructGraph();

        int res = 0;
        this.visited = new boolean[G.length];
        // 求连通分量的最大节点数
        for (int v = 0; v < G.length; v++) {
            int x = v / C, y = v % C;

            if (!visited[v] && grid[x][y] == 1) {
                res = Math.max(res, dfs(v));
            }
        }

        return res;

    }

    private HashSet<Integer>[] constructGraph() {
        HashSet<Integer>[] g = new HashSet[R * C];
        for (int i = 0; i < g.length; i++) {
            g[i] = new HashSet<>();
        }

        for (int v = 0; v < g.length; v++) {
            int x = v / C, y = v % C;
            if (grid[x][y] == 1) {
                for (int d = 0; d < 4; d++) {
                    int nextx = x + dirs[d][0];
                    int nexty = y + dirs[d][1];
                    if (inArea(nextx, nexty) && grid[nextx][nexty] == 1) {
                        int next = nextx * C + nexty;
                        g[v].add(next);
                        g[next].add(v);
                    }
                }
            }
        }

        return g;
    }

    private int dfs(int v) {
        visited[v] = true;
        // 顶点v所在的连通分量的节点数目
        int res = 1;
        for (int w : G[v]) {
            if (!visited[w]) {
                res += dfs(w);
            }
        }
        return res;
    }

    boolean inArea(int x, int y) {
        return x >= 0 && x < R && y >= 0 && y < C;
    }
}

方法2:利用题目自身的数据结构,不自己建图,通过题目提供的数据结构去求得邻接关系。

// Java 参考代码
class Solution {

    private int R, C;
    private int[][] grid;
    private int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    private boolean[][] visited;
    public int maxAreaOfIsland(int[][] grid) {
        if (grid == null) return 0;
        R = grid.length;
        if (R == 0) return 0;
        
        C = grid[0].length;
        if (C == 0) return 0;

        this.grid = grid;
        
        int res = 0;
        this.visited = new boolean[R][C];

        // 求连通分量的最大节点数
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if (!visited[i][j] && grid[i][j] == 1) {
                    res = Math.max(res, dfs(i, j));
                }
            }
            
        }
        return res;
    }

    private int dfs(int x, int y) {
        visited[x][y] = true;
        // 顶点v所在的连通分量的节点数目
        int res = 1;

        // 对四个方向找邻接点
        for (int d = 0; d < 4; d++) {
            int nextx = x + dirs[d][0];
            int nexty = y + dirs[d][1];
            
            if (inArea(nextx, nexty) && grid[nextx][nexty] == 1) {
                if (!visited[nextx][nexty]) {
                    res += dfs(nextx, nexty);
                }
            }
        }

        return res;
    }

    boolean inArea(int x, int y) {
        return x >= 0 && x < R && y >= 0 && y < C;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 二分法 5 1.1. 什么是二分查找 5 1.2. 如何识别二分法 5 1.3. 二分法模板 6 1.3.1. 模板一 6 1.3.1.1. 模板代码 6 1.3.1.2. 关键属性 7 1.3.1.3. 语法说明 7 1.3.1.4. Lc69:x的平方根 8 1.3.1.5. Lc374:猜数大小 9 1.3.1.6. Lc33:搜索旋转数组 11 1.3.2. 模板二 13 1.3.2.1. 模板代码 13 1.3.2.2. 关键属性 14 1.3.2.3. 语法说明 14 1.3.2.4. Lc278:第一个错误版本 14 1.3.2.5. Lc162:寻找峰值 16 1.3.2.6. Lc153:寻找旋转排序数组最小值 19 1.3.2.7. Lc154:寻找旋转排序数组最小值II 20 1.3.3. 模板三 22 1.3.3.1. 模板代码 22 1.3.3.2. 关键属性 23 1.3.3.3. 语法说明 23 1.3.3.4. LC-34:在排序数组中查找元素的第一个和最后一个 23 1.3.3.5. LC-658:找到K个最接近的元素 25 1.3.4. 小结 28 1.4. LeetCode中二分查找目 29 2. 双指针 30 2.1. 快慢指针 31 2.1.1. 什么是快慢指针 31 2.1.2. 快慢指针模板 31 2.1.3. 快慢指针相关目 32 2.1.3.1. LC-141:链表是否有环 32 2.1.3.2. LC-142:环形链表入口 34 2.1.3.3. LC-876:链表的中间节点 37 2.1.3.4. LC-287:寻找重复数 40 2.2. 滑动窗口 43 2.2.1. 什么是滑动窗口 43 2.1.4. 常见题型 44 2.1.5. 注意事项 45 2.1.6. 滑动窗口模板 45 2.1.7. 滑动窗口相关目 46 2.1.7.1. LC-3:无重复字符的最长子串 47 2.1.7.2. LC-76:最小覆盖子串 49 2.1.7.3. LC-209:长度最小的子数组 54 2.1.7.4. LC-239:滑动窗口最大值 57 2.1.7.5. LC-395:至少有K个重复字符的最长子串 60 2.1.7.6. LC-567:字符串排列 62 2.1.7.7. LC-904:水果成篮 64 2.1.7.8. LC-424:替换后的最长重复字符 66 2.1.7.9. LC-713:乘积小于K的子数组 67 2.1.7.10. LC-992:K个不同整数的子数组 70 2.3. 左右指针 73 2.3.1. 模板 73 2.3.2. 相关目 73 2.3.2.1. LC-76:删除倒数第N个节点 74 2.3.2.2. LC-61:旋转链表 76 2.3.2.3. LC-80:删除有序数组中的重复项 79 2.3.2.4. LC-86:分割链表 80 2.3.2.5. LC-438:找到字符串中所有字母的异位词 82 3. 模板 85 2.3.2.6. LC-76:删除倒数第N个节点 85
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值