菜鸡的原地踏步史08(◐‿◑)

技巧题

只出现一次的数字

class Solution {
    /** 
        HashMap统计num和次数
        前面一天用堆魔怔了,也用堆进行排列...
    */
    public int singleNumber(int[] nums) {
        PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        for(var node : map.entrySet()) {
            int[] temp = new int[2];
            temp[0] = node.getKey();
            temp[1] = node.getValue();
            pq.offer(temp);
        }
        return pq.peek()[0];
    }
}

多数元素

class Solution {
    /** 
        HashMap统计?
    */
    public int majorityElement(int[] nums) {
        int res = 0;
        int n = nums.length / 2;
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int num: nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        for(var node: map.entrySet()) {
            if(node.getValue() > n) {
                res =  node.getKey();
            }
        }
        return res;
    }
}

颜色分类

class Solution {
    /** 
        使用堆不就秒了?
    */
    public void sortColors(int[] nums) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int num : nums) {
            pq.offer(num);
        }
        int i = 0;
        while(!pq.isEmpty()) {
            nums[i] = pq.poll();
            i++;
        }
    }
}

芜湖~ 力扣200题打卡!

下一个排列

class Solution {
    /** 
    i = 0 1 2 3
        2 1 4 3 从后向前搜索
        2|1 3 4 搜到第一个i > i - 1的,记录i,排序i到末尾
        2|3 1 4 找到排序后第一个 > i - 1的,交换

        需要注意Arrays.sort(nums, i, j) 是左闭右开的区间
        对nums进行从i到末尾的sort需要(nums, i, len(而不是len - 1))
    */
    public void nextPermutation(int[] nums) {
        int len = nums.length;
        for(int i = len - 1; i >= 0; i--) {
           if( i > 0 && nums[i] > nums[i - 1]) {
                Arrays.sort(nums, i, len);
                for(int j = i; j < len; j++) {
                    if(nums[j] > nums[i - 1]) {
                        int temp = nums[i - 1];
                        nums[i - 1] = nums[j];
                        nums[j] = temp;
                        return;
                    }
                }
            }
            else if(i == 0) {
                Arrays.sort(nums);
            } 
        }
    }
}

寻找重复数

class Solution {
    /** 
        hashmap直接秒了啊
    */
    public int findDuplicate(int[] nums) {
        int res = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int num: nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        for(var node: map.entrySet()) {
            if(node.getValue() > 1) {
                res = node.getKey();
            }
        }
        return res;
    }
}

图论

岛屿数量

class Solution {
    /** 
        深度优先搜索dfs
        类似和寻找字符串
        逐行逐列遍历判断
    */
    public int numIslands(char[][] grid) {
        int row = grid.length;
        int col = grid[0].length;
        int island = 0;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(grid[i][j] == '1') {
                    island++;
                    dfs(grid, i, j);
                }
            }
        }
        return island;
    }
    public void dfs(char[][] grid, int i, int j) {
        int row = grid.length;
        int col = grid[0].length;
        if(i >= row || i < 0 || j >= col || j < 0 || grid[i][j] == '0') {
            return;
        }
        grid[i][j] = '0';
        dfs(grid, i + 1, j);
        dfs(grid, i, j + 1);
        dfs(grid, i - 1, j);
        dfs(grid, i, j - 1);
    }
}

腐烂的橘子

class Solution {
    /** 
        用2覆盖grid网格
        2表示初始化时间
    */
    public int orangesRotting(int[][] grid) {
        if(grid == null || grid.length == 0) {
            return 0;
        }
        int row = grid.length;
        int col = grid[0].length;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(grid[i][j] == 2) {
                    dfs(grid, i, j, 2);
                }
            }
        }
        int maxtime = 0;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(grid[i][j] == 1) {
                    return -1;
                }
                else {
                    maxtime = Math.max(maxtime, grid[i][j]);
                }
            }
        }
        return maxtime == 0 ? 0 : maxtime - 2;
    }
    public void dfs(int[][] grid, int i, int j, int time) {
        int row = grid.length;
        int col = grid[0].length;
        if(i >= row || i < 0 || j >= col || j < 0) {
            return;
        }
        if(grid[i][j] != 1 && grid[i][j] < time) { // 没新鲜橘子,且时间更少,倒回去
            return;
        }
        grid[i][j] = time;

        dfs(grid, i + 1, j, time + 1);
        dfs(grid, i, j + 1, time + 1);
        dfs(grid, i - 1, j, time + 1);
        dfs(grid, i, j - 1, time + 1);
    }
}

课程表


实现Trie 前缀树

class Trie {
    Map<Character, Map> map, t;
    public Trie() {
        map = new HashMap<>();
    }
    
    public void insert(String word) {
        t = map;
        for(char w: word.toCharArray()) {
            if(!t.containsKey(w)) {
                t.put(w, new HashMap<Character, Map>());
            }
            t = t.get(w);
        }
        t.put('#', null);
    }
    
    public boolean search(String word) {
        t = map;
        for(char w: word.toCharArray()) {
            if(!t.containsKey(w)) {
                return false;
            }
            t = t.get(w);
        }
        return t.containsKey('#');
    }
    
    public boolean startsWith(String prefix) {
        t = map;
        for(char w: prefix.toCharArray()) {
            if(!t.containsKey(w)) {
                return false;
            }
            t = t.get(w);
        }
        return true;
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */

相关推荐

  1. 原地踏步08(◐‿◑)

    2024-07-14 08:22:03       27 阅读
  2. 原地踏步06(◐‿◑)

    2024-07-14 08:22:03       24 阅读
  3. 原地踏步07(◐‿◑)

    2024-07-14 08:22:03       17 阅读
  4. Leetcode(02

    2024-07-14 08:22:03       24 阅读
  5. 常见网络问题汇总】之:网络丢包

    2024-07-14 08:22:03       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 08:22:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 08:22:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 08:22:03       58 阅读
  4. Python语言-面向对象

    2024-07-14 08:22:03       69 阅读

热门阅读

  1. Elasticsearch 角色和权限管理

    2024-07-14 08:22:03       24 阅读
  2. Git配置笔记

    2024-07-14 08:22:03       30 阅读
  3. Docker安装Zookeeper、RocketMQ

    2024-07-14 08:22:03       26 阅读
  4. 计算1的数量

    2024-07-14 08:22:03       28 阅读
  5. 特斯拉的选择:.NET技术栈的工业级魅力

    2024-07-14 08:22:03       20 阅读
  6. 1、ASP安全

    2024-07-14 08:22:03       22 阅读
  7. 数据结构第24节 二分查找

    2024-07-14 08:22:03       19 阅读
  8. QComboBox

    2024-07-14 08:22:03       21 阅读
  9. 【PHP】Symfony框架

    2024-07-14 08:22:03       31 阅读
  10. qt 可以滚动区域实验举例

    2024-07-14 08:22:03       24 阅读
  11. win10 cpu 下使用Xinference来进行大模型的推理测试

    2024-07-14 08:22:03       24 阅读